RecyclerView自定义滚动

时间:2017-02-28 09:24:47

标签: android android-recyclerview

Horizontal scroll with the above effect

我想将滚动设置为图像上方。我的问题陈述始终是最右边的图像将比RecyclerView的其他项目更大。示例代码将有助于我实现我的解决方案。 例如:滚动后,下一张图像将位于较大尺寸的最右侧位置。

public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private RecyclerView mRecyclerView;
private LoungeImageAdapter loungeImageAdapter;
//private RecyclerView.LayoutManager mLayoutManager;
private List<LoungeImageModel> imageModelList = new ArrayList<>();
private LinearLayoutManager llm;

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

    //prepare image dataset
    imageModelList.add(new LoungeImageModel(R.drawable.a));
    imageModelList.add(new LoungeImageModel(R.drawable.d));
    imageModelList.add(new LoungeImageModel(R.drawable.c));
    imageModelList.add(new LoungeImageModel(R.drawable.d));
    imageModelList.add(new LoungeImageModel(R.drawable.e));
    imageModelList.add(new LoungeImageModel(R.drawable.f));

    //recyclerview setup
    mRecyclerView = (RecyclerView) findViewById(R.id.loungeImageRV);
    mRecyclerView.setHasFixedSize(true);
    //mLayoutManager = new LinearLayoutManager(this);
    llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.HORIZONTAL);
    llm.setReverseLayout(true);
    loungeImageAdapter = new LoungeImageAdapter(getApplicationContext(), imageModelList);
    mRecyclerView.setAdapter(loungeImageAdapter);
    //mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setLayoutManager(llm);
    loungeImageAdapter.notifyDataSetChanged();

    //offset task starts here
    computeHorizontalScrollOffset(mRecyclerView);
}


public int computeHorizontalScrollOffset(RecyclerView recyclerView){//RecyclerView.State
    //llm = (LinearLayoutManager) recyclerView.getLayoutManager();

    int position = llm.findFirstVisibleItemPosition();
    //RecyclerView.ViewHolder mViewHolder = (RecyclerView.ViewHolder) recyclerView.findViewHolderForAdapterPosition(position);
    RecyclerView.ViewHolder mViewHolder = recyclerView.findViewHolderForAdapterPosition(position);
    View item = mViewHolder.itemView;

    int y = (int) item.getY();
    if (y < 0) y *= -1;

    if (position == 0) return y;
    else {
        int offset = y;
        for (int i = 0; i < position; i++) {
            //Add your previous item heights to offset
            Log.e("DKS","Ting-tong");
        }
        return offset;
    }
}

}

  

以下是适配器类

public class LoungeImageAdapter extends RecyclerView.Adapter<LoungeImageAdapter.ImageAdapterViewHolder>{
Context context;
List<LoungeImageModel> imageModelList;

public LoungeImageAdapter(Context context, List<LoungeImageModel> imageModelList) {
    this.context = context;
    this.imageModelList = imageModelList;
}

@Override
public ImageAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item, parent, false);
    return new ImageAdapterViewHolder(itemView);
}

@Override
public void onBindViewHolder(ImageAdapterViewHolder holder, int position) {
    holder.itemImageView.setImageResource(imageModelList.get(position).getLoungeItemImage());
}

@Override
public int getItemCount() {
    return imageModelList.size();
}

public class ImageAdapterViewHolder extends RecyclerView.ViewHolder {
    public ImageView itemImageView;
    public ImageAdapterViewHolder(View itemView) {
        super(itemView);
        itemImageView = (ImageView) itemView.findViewById(R.id.itemIV);
        context = itemView.getContext();
    }
}

}

1 个答案:

答案 0 :(得分:0)

您总是应该将以下代码作为您的recyclerview项目。

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/yourimage"/>