同步滚动不同宽度的RecyclerViews

时间:2017-06-27 03:53:15

标签: android android-recyclerview android-adapter

我有一个应用存储某些到期日期,并将其显示在 MainActivity 中。

我正在努力完成第一张图片上的内容,但我还没有。

Design Required

到目前为止我得到的是:

Gif

我的问题:

  • 当向右或向左单击时,如何剪切 First RecyclerView 以保持在中心位置? (我解决了这个问题后,我将继续处理动画,用于处理bg颜色和粗体文字)
  • 如何同步两个RecyclerViews 以在点击时更改为所需的位置?

这就是我的尝试:

这些工作或多或少,在第一个链接上我设法获得了位置,但不知道如何处理该职位;在第二个链接上它确实同步,但我需要在点击时滚动到位置。 SIDE-NOTE:我在两个RecyclerView上都使用 PagerSnapHelper

如果您需要任何其他活动任何反馈,请发表评论!

public class Home extends BaseActivity {

private DBHelper mDBHelper;
private Context mContext;
private RecyclerView mRecyclerViewDetails, mRecyclerViewSymbols;
private HomeDetailsAdapter mDetailsAdapter;
private HomeSymbolsAdapter mSymbolsAdapter;
private LinearLayoutManager mLinearLayoutManager;
private LinearLayoutPagerManager mLinearLayoutPagerManager;

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

    mDBHelper = new DBHelper(getApplicationContext());
    mRecyclerViewDetails = (RecyclerView) findViewById(R.id.recycler_view_details);
    mRecyclerViewSymbols = (RecyclerView) findViewById(R.id.recycler_view_symbols);

    mSymbolsAdapter = new HomeSymbolsAdapter(mContext, mDBHelper.getAllDates(), new HomeSymbolsAdapter.clickCallback() {
        @Override
        public void onItemClick(int position) {
            mRecyclerViewSymbols.scrollToPosition(position);
            Log.d("Home", String.valueOf(position));
        }
    });

    mRecyclerViewSymbols.setAdapter(mSymbolsAdapter);
    mLinearLayoutPagerManager = new LinearLayoutPagerManager(mContext, LinearLayoutManager.HORIZONTAL, false, 3);
    mRecyclerViewSymbols.setLayoutManager(mLinearLayoutPagerManager);
    mRecyclerViewSymbols.getLayoutManager().scrollToPosition(Integer.MAX_VALUE / 2);

    mDetailsAdapter = new HomeDetailsAdapter(mContext, mDBHelper.getAllDates());

    mRecyclerViewDetails.setAdapter(mDetailsAdapter);
    mLinearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
    mRecyclerViewDetails.setLayoutManager(mLinearLayoutManager);
    mRecyclerViewDetails.getLayoutManager().scrollToPosition(Integer.MAX_VALUE / 2);

    PagerSnapHelper snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(mRecyclerViewDetails);
    PagerSnapHelper snapHelper2 = new PagerSnapHelper();
    snapHelper2.attachToRecyclerView(mRecyclerViewSymbols);

}

HomeSymbolsAdapter

public class HomeSymbolsAdapter extends RecyclerView.Adapter<HomeSymbolsAdapter.ViewHolder> {

private Context mContext;
private DBHelper mDBHelper;
private ArrayList<Date> mDatesList;
private clickCallback mClickCallback;
private final static int ITEMS_PER_PAGE = 3;

public HomeSymbolsAdapter(Context context, ArrayList<Date> datesList, clickCallback clickCallback) {
    mContext = context;
    mDatesList = datesList;
    mClickCallback = clickCallback;
}

public interface clickCallback{
    void onItemClick(int position);
}

@Override
public HomeSymbolsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.format_home_symbols, parent, false));
}

@Override
public void onBindViewHolder(HomeSymbolsAdapter.ViewHolder holder, int position) {
    int positionInList = position % mDatesList.size();
    Date date = mDatesList.get(positionInList);
    holder.mSymbol.setText(date.getSymbol());

}

@Override
public int getItemCount() {
    return Integer.MAX_VALUE;
}

public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView mSymbol;

    public ViewHolder(View view) {
        super(view);

        mSymbol = (TextView) view.findViewById(R.id.symbols_tv);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickCallback.onItemClick(getAdapterPosition());
            }
        });

    }
}

HomeDetailsAdapter

public class HomeDetailsAdapter extends RecyclerView.Adapter<HomeDetailsAdapter.ViewHolder> {

private Context mContext;
private ArrayList<Date> mDates;

public HomeDetailsAdapter(Context context, ArrayList<Date> dates) {
    mContext = context;
    mDates = dates;
}

@Override
public HomeDetailsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.format_home, parent, false));
}

@Override
public void onBindViewHolder(HomeDetailsAdapter.ViewHolder holder, int position) {
    int positionInList = position % mDates.size();
    Date date = mDates.get(positionInList);
    holder.mName.setText(date.getName());
    holder.mDaysLeft.setText("125");
    holder.mDays.setText(R.string.days);
    holder.mDate.setText(date.getDate());
}

@Override
public int getItemCount() {
    return Integer.MAX_VALUE;
}

public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView mName, mDaysLeft, mDays, mDate;

    public ViewHolder(View view) {
        super(view);

        mName = (TextView) view.findViewById(R.id.format_home_name);
        mDaysLeft = (TextView) view.findViewById(R.id.format_home_days_left);
        mDays = (TextView) view.findViewById(R.id.format_home_days);
        mDate = (TextView) view.findViewById(R.id.format_home_date);
    }
}

0 个答案:

没有答案