Skip to content

Commit

Permalink
同步主分支的demo
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Apr 20, 2020
1 parent fa933dc commit 3471a52
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 6 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 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<activity android:name=".ConsecutiveActivity"></activity>
<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 @@ -7,6 +7,7 @@

import com.donkingliang.consecutivescrollerdemo.adapter.RecyclerViewAdapter;


public class ConsecutiveActivity extends AppCompatActivity {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,31 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.sample).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SampleActivity.class);
Intent intent = new Intent(MainActivity.this, SampleActivity.class);
startActivity(intent);
}
});

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

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

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);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.donkingliang.consecutivescrollerdemo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.donkingliang.consecutivescrollerdemo.adapter.RecyclerViewAdapter;

/**
* @Author donkingliang
* @Description
* @Date 2020/4/18
*/
public class MyFragment extends Fragment {

@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);

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

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

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.donkingliang.consecutivescrollerdemo.adapter.TabPagerAdapter;

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

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的下级view的滑动事件。\n" +
"下面的例子中,通过自定义ViewPager,实现IConsecutiveScroller接口,ConsecutiveScrollerLayout能正确的处理ViewPager里" +
"的RecyclerView滑动,使RecyclerView与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,39 @@
package com.donkingliang.consecutivescrollerdemo.adapter;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import java.util.List;


/**
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.donkingliang.consecutivescrollerdemo.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;


import com.donkingliang.consecutivescroller.IConsecutiveScroller;

import java.util.ArrayList;
import java.util.List;
/**
* @Author donkingliang
* @Description
* @Date 2020/4/18
*/
public class MyViewPager extends ViewPager implements IConsecutiveScroller {
public MyViewPager(@NonNull Context context) {
super(context);
}

public MyViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

/**
* 返回当前需要滑动的view。
* 注意:这个view不一定是ViewPager的直接子view,使用者应该根据自己的业务情况返回需要滑动的下级view。
* @return
*/
@Override
public View getCurrentScrollerView() {
int count = getChildCount();
for (int i = 0; i < count; i++) {
View view = getChildAt(i);
if (view.getX() == getScrollX()) {
return view;
}
}
return this;
}

/**
* 返回全部需要滑动的下级view
* @return
*/
@Override
public List<View> getScrolledViews() {
List<View> views = new ArrayList<>();
int count = getChildCount();
if (count > 0) {
for (int i = 0; i < count; i++) {
views.add(getChildAt(i));
}
} else {
views.add(this);
}
return views;
}
}
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>
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_sticky.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
</LinearLayout>
</android.support.v4.widget.NestedScrollView>


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
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" />

<android.support.design.widget.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>
20 changes: 20 additions & 0 deletions app/src/main/res/layout/fragment_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/item_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />

<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />
</LinearLayout>
7 changes: 7 additions & 0 deletions app/src/main/res/layout/fragment_item_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.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" />
4 changes: 4 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_margin">16dp</dimen>
</resources>

0 comments on commit 3471a52

Please sign in to comment.