Теперь есть такой спрос, RecyclerView внутри каждого элемента вложен пейджер с вертикальной прокруткой, а просмотрпейджер автоматически запускает бесконечный цикл. Я использую вложенный RecyclerView RecyclerView для достижения эффекта скользящего просмотра пейджера. Я использовал RecyclerView smoothScrollToPosition для достижения. Но в этом процессе я столкнулся с проблемой: несколько раз скользит вперед и назад за пределы скольжения RecyclerView, внутренний SmoothScrollToPosition RecyclerViwe будет скользить хаосом. Если сделано то же требование. Неполадки
public class MainActivity extends AppCompatActivity {
private RecyclerView mRvOuter;
private LinearLayoutManager linearLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRvOuter = (RecyclerView) findViewById(R.id.rv_outer);
linearLayoutManager = new LinearLayoutManager(this);
mRvOuter.setLayoutManager(linearLayoutManager);
mRvOuter.setAdapter(new OuterAdapter(this));
}
}
Макет MainActivity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_outer"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
Внешний адаптер:
public class OuterAdapter extends RecyclerView.Adapter<OuterAdapter.OuterViewHolder> {
LayoutInflater layoutInflater;
List<String> mDatas;
Context mContext;
public OuterAdapter(Context context) {
mContext = context;
layoutInflater = LayoutInflater.from(context);
}
@Override
public OuterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
OuterViewHolder viewHolder = new OuterViewHolder(layoutInflater.inflate(R.layout.item_outer, parent, false));
return viewHolder;
}
@Override
public void onBindViewHolder(final OuterViewHolder holder, int position) {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
holder.mRvInner.setLayoutManager(linearLayoutManager);
holder.mRvInner.setAdapter(new InnerAdapter(mContext, initDada(position)));
holder.mRvInner.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return e.getAction() == MotionEvent.ACTION_MOVE;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
final Handler handler = new Handler();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
SystemClock.sleep(2000);
handler.post(new Runnable() {
@Override
public void run() {
holder.mRvInner.smoothScrollToPosition(holder.i++);
}
});
}
}
});
thread.start();
}
@Override
public int getItemCount() {
return 30;
}
public class OuterViewHolder extends RecyclerView.ViewHolder {
RecyclerView mRvInner;
int i;
public OuterViewHolder(View itemView) {
super(itemView);
mRvInner = (RecyclerView) itemView.findViewById(R.id.rv_inner);
}
}
private List<String> initDada(int i) {
mDatas = new ArrayList<String>();
for (int j = 0; j <= i; j++) {
mDatas.add("" + j);
}
return mDatas;
}
}
InnerAdapter:
public class InnerAdapter extends RecyclerView.Adapter<InnerAdapter.InnerViewHolder> {
private List<String> mInnerDatas;
private LayoutInflater mInflater;
public InnerAdapter(Context context, List<String> innerDatas) {
mInflater = LayoutInflater.from(context);
mInnerDatas = innerDatas;
}
@Override
public InnerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
InnerViewHolder viewHolder = new InnerViewHolder(mInflater.inflate(R.layout.item_inner, parent, false));
return viewHolder;
}
@Override
public void onBindViewHolder(InnerViewHolder holder, int position) {
holder.mTvInner.setText(mInnerDatas.get(position % mInnerDatas.size()));
}
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
public class InnerViewHolder extends RecyclerView.ViewHolder {
TextView mTvInner;
public InnerViewHolder(View itemView) {
super(itemView);
mTvInner = (TextView) itemView.findViewById(R.id.tv_inner);
}
}
}
Внешний вид элемента:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="center"
android:text="i am belong outerRecyclerView"
android:textSize="20sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_inner"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="15dp"
android:background="#ffaadd"></android.support.v7.widget.RecyclerView>
</LinearLayout>
item inner:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_inner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#000"
android:textSize="30sp" />
</LinearLayout>
ViewPager
? Можешь переписать попонятнее? Вы получите помощь быстрее. - person Шах   schedule 19.05.2016