Skip to content

Commit

Permalink
添加ViewPager demo
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Apr 20, 2020
1 parent f11928e commit a77f015
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 88 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ConsecutiveActivity"></activity>
<activity android:name=".ConsecutiveActivity"/>
<activity android:name=".StickyActivity" />
<activity android:name=".SampleActivity" />
<activity android:name=".ViewPagerActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public void onClick(View v) {
}
});

findViewById(R.id.btn_viewpager).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,ViewPagerActivity.class);
startActivity(intent);
}
});

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,25 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout;
import com.donkingliang.consecutivescrollerdemo.adapter.RecyclerViewAdapter;
import com.donkingliang.consecutivescrollerdemo.widget.MyRecyclerView;

/**
* @Author teach-梁任彦
* @Author donkingliang
* @Description
* @Date 2020/4/18
*/
public class MyFragment extends Fragment {

private ConsecutiveScrollerLayout mScrollerLayout;

public MyFragment(ConsecutiveScrollerLayout scrollerLayout) {
this.mScrollerLayout = scrollerLayout;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_item_list,container,false);

MyRecyclerView list = view.findViewById(R.id.list);
RecyclerView list = view.findViewById(R.id.list);
list.setLayoutManager(new LinearLayoutManager(getContext()));
RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(),"ViewPager1-");
list.setAdapter(adapter);
list.setScrollerLayout(mScrollerLayout);
return view;

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.donkingliang.consecutivescrollerdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.webkit.WebChromeClient;
Expand Down Expand Up @@ -44,24 +41,10 @@ public void onProgressChanged(WebView view, int newProgress) {
RecyclerViewAdapter adapter1 = new RecyclerViewAdapter(this,"RecyclerView1-");
recyclerView1.setAdapter(adapter1);

// RecyclerView recyclerView2 = findViewById(R.id.recyclerView2);
// recyclerView2.setLayoutManager(new LinearLayoutManager(this));
// RecyclerViewAdapter adapter2 = new RecyclerViewAdapter(this,"RecyclerView2-");
// recyclerView2.setAdapter(adapter2);

ViewPager viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return new MyFragment(scrollerLayout);
}

@Override
public int getCount() {
return 4;
}
});

RecyclerView recyclerView2 = findViewById(R.id.recyclerView2);
recyclerView2.setLayoutManager(new LinearLayoutManager(this));
RecyclerViewAdapter adapter2 = new RecyclerViewAdapter(this,"RecyclerView2-");
recyclerView2.setAdapter(adapter2);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.donkingliang.consecutivescrollerdemo;

import android.os.Bundle;
import android.widget.TextView;

import com.donkingliang.consecutivescrollerdemo.adapter.TabPagerAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

public class ViewPagerActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);

TextView text = findViewById(R.id.text);
text.setText("子view实现IConsecutiveScroller接口,并通过实现接口方法告诉ConsecutiveScrollerLayout需要滑动的下级view,\n" +
" * ConsecutiveScrollerLayout就能正确地处理它的滑动事件。");
ViewPager viewPager = findViewById(R.id.viewPager);
TabLayout tabLayout = findViewById(R.id.tabLayout);
viewPager.setAdapter(new TabPagerAdapter(getSupportFragmentManager(), getTabs(), getFragments()));
tabLayout.setupWithViewPager(viewPager);
}

private List<String> getTabs() {
List<String> tabs = new ArrayList<>();
tabs.add("Tab1");
tabs.add("Tab2");
tabs.add("Tab3");
tabs.add("Tab4");
tabs.add("Tab5");
return tabs;
}

private List<Fragment> getFragments() {
List<Fragment> fragmentList = new ArrayList<>();
fragmentList.add(new MyFragment());
fragmentList.add(new MyFragment());
fragmentList.add(new MyFragment());
fragmentList.add(new MyFragment());
fragmentList.add(new MyFragment());
return fragmentList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.donkingliang.consecutivescrollerdemo.adapter;


import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

/**
* Depiction: TabLayout 和 Fragment,viewpager结合使用的viewpager adapter。
*/
public class TabPagerAdapter extends FragmentStatePagerAdapter {

private List<String> mTitles;
private List<? extends Fragment> mFragments;

public TabPagerAdapter(FragmentManager fm, List<String> titleList, List<? extends Fragment> fragments) {
super(fm);
this.mTitles = titleList;
this.mFragments = fragments;
}

@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}

@Override
public int getCount() {
return mFragments == null ? 0 : mFragments.size();
}

@Override
public CharSequence getPageTitle(int position) {
return mTitles == null ? "" : mTitles.get(position);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.List;

/**
* @Author teach-梁任彦
* @Author donkingliang
* @Description
* @Date 2020/4/18
*/
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/sample"
Expand All @@ -23,4 +23,11 @@
android:layout_height="wrap_content"
android:text="局部滑动" />

<Button
android:id="@+id/btn_viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="支持ViewPager"
android:textAllCaps="false" />

</LinearLayout>
14 changes: 7 additions & 7 deletions app/src/main/res/layout/activity_sticky.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
android:text="吸顶View - 1"
android:textColor="@android:color/black"
android:textSize="18sp"
app:layout_isSticky="false" />
app:layout_isSticky="true" />

<WebView
android:id="@+id/webView"
Expand All @@ -26,7 +26,7 @@
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"
app:layout_isSticky="false">
app:layout_isSticky="true">

<TextView
android:layout_width="match_parent"
Expand All @@ -51,7 +51,7 @@
android:text="吸顶View - 3"
android:textColor="@android:color/black"
android:textSize="18sp"
app:layout_isSticky="false" />
app:layout_isSticky="true" />

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
Expand Down Expand Up @@ -123,11 +123,11 @@
android:text="吸顶View - 4"
android:textColor="@android:color/black"
android:textSize="18sp"
app:layout_isSticky="false" />
app:layout_isSticky="true" />

<com.donkingliang.consecutivescrollerdemo.widget.MyViewPager
android:id="@+id/viewPager"
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="wrap_content" />

</com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
35 changes: 35 additions & 0 deletions app/src/main/res/layout/activity_viewpager.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrollerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="10dp"
android:textColor="@android:color/black"
android:textSize="18sp" />

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
app:layout_isSticky="true"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorPrimary"
app:tabIndicatorHeight="3dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/colorPrimary" />

<com.donkingliang.consecutivescrollerdemo.widget.MyViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_item_list.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<com.donkingliang.consecutivescrollerdemo.widget.MyRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"/>
android:layout_marginRight="16dp" />

0 comments on commit a77f015

Please sign in to comment.