Joe*_*ard 16 android android-layout android-recyclerview
我试图在RecyclerView中显示确切的(例如3项).我们的Web应用程序显示如下:
如果我移动物品:
在Android中,我使用RecyclerView显示图像:
如何准确显示RecyclerView可见区域中的3个项目?如何避免显示"半"项目?这可能吗?
RecyclerView:
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="130dp"
android:id="@+id/rvProducts" />
Run Code Online (Sandbox Code Playgroud)
回收站视图中的一项:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/ivProduct"
android:src="@drawable/noimage"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
码:
LinearLayoutManager layoutManager
= new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
RecyclerView productImageList = (RecyclerView) view.findViewById(R.id.rvProducts);
productImageList.setLayoutManager(layoutManager);
GalleryRecyclerAdapter adapter = new GalleryRecyclerAdapter(images);
productImageList.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
Pro*_*kky 14
一种解决方案是以width编程方式设置每个孩子.
view.getLayoutParams().width = getScreenWidth() / VIEWS_COUNT_TO_DISPLAY;
Run Code Online (Sandbox Code Playgroud)
屏幕尺寸如下:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics).widthPixels;
Run Code Online (Sandbox Code Playgroud)
Rav*_*aha 10
我已经按照以下方法完成了上述任务,请参考以下代码
根据我的onCreateViewHolder()方法RecycleView Adapter,我找出屏幕的宽度,然后分成3个部分.请检查一次.
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.MY_ITEM_TO_INFALTE, parent, false);
itemView.getLayoutParams().width = (int) (getScreenWidth() / NUMBERS_OF_ITEM_TO_DISPLAY); /// THIS LINE WILL DIVIDE OUR VIEW INTO NUMBERS OF PARTS
return new MyCreationItemAdapter.MyViewHolder(itemView);
}
Run Code Online (Sandbox Code Playgroud)
以上getScreenWidth()我们在上面使用的代码方法如下,请检查.
public int getScreenWidth() {
WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size.x;
}
Run Code Online (Sandbox Code Playgroud)