以下是我构建循环视图的方法
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
ItemData itemsData[] = { new ItemData("Help",R.drawable.profile_pic),
new ItemData("Delete",R.drawable.profile_pic),
new ItemData("Cloud",R.drawable.profile_pic),
new ItemData("Favorite",R.drawable.profile_pic),
new ItemData("Like",R.drawable.profile_pic),
new ItemData("Rating",R.drawable.profile_pic)};
LinearLayoutManager l = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(l);
MyAdapter mAdapter = new MyAdapter(itemsData);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
它有效,但问题是它每次只显示1项,如何更改为2项?这意味着,图标应为屏幕宽度的50%,每页总共有2个图标
喜欢这个(不需要分隔符):
icon1 | icon2
XML(主要活动)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context="com.hmkcode.android.recyclerview.MainActivity" >
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
/>
</RelativeLayout>
XML(项目)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp">
<!-- icon -->
<ImageView
android:id="@+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/profile_pic" />
<!-- title -->
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#000000"
android:textSize="22sp" />
</RelativeLayout>
感谢您的帮助
更新:
注意该应用只有potarit方向,所以可以忽略横向
的情况目前的结果:
并且GridLayoutManager不是确切的行为
GridLayoutManager g = new GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false);
答案 0 :(得分:1)
你应该扩展LinearLayoutManager并覆盖下面的tow方法:
/**
* Measure a child view using standard measurement policy, taking the padding
* of the parent RecyclerView and any added item decorations into account.
*
* <p>If the RecyclerView can be scrolled in either dimension the caller may
* pass 0 as the widthUsed or heightUsed parameters as they will be irrelevant.</p>
*
* @param child Child view to measure
* @param widthUsed Width in pixels currently consumed by other views, if relevant
* @param heightUsed Height in pixels currently consumed by other views, if relevant
*/
public void measureChild(View child, int widthUsed, int heightUsed) {
RecyclerView.LayoutParams lpTmp = (RecyclerView.LayoutParams) child.getLayoutParams();
final RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(screenWidth/2,lpTmp.height);
int widthSpec = View.MeasureSpec.makeMeasureSpec(MainActivity.screenWidth /2 , View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(lp.height , View.MeasureSpec.EXACTLY);
child.measure(widthSpec, heightSpec);
}
/**
* Measure a child view using standard measurement policy, taking the padding
* of the parent RecyclerView, any added item decorations and the child margins
* into account.
*
* <p>If the RecyclerView can be scrolled in either dimension the caller may
* pass 0 as the widthUsed or heightUsed parameters as they will be irrelevant.</p>
*
* @param child Child view to measure
* @param widthUsed Width in pixels currently consumed by other views, if relevant
* @param heightUsed Height in pixels currently consumed by other views, if relevant
*/
public void measureChildWithMargins(View child, int widthUsed, int heightUsed) {
super.measureChildWithMargins(child,widthUsed,heightUsed);
RecyclerView.LayoutParams lpTmp = (RecyclerView.LayoutParams) child.getLayoutParams();
final RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(screenWidth/2,lpTmp.height);
int widthSpec = View.MeasureSpec.makeMeasureSpec(MainActivity.screenWidth / 2, View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(lp.height, View.MeasureSpec.EXACTLY);
child.measure(widthSpec, heightSpec);
}
上面的代码非常简单,只是一个演示,数字 2 。您可以根据需要自定义代码。希望这些对你有帮助;
答案 1 :(得分:1)
使用此类而不是LinearLayoutManager。
import android.content.Context;
import android.graphics.Point;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
/**
* Created by Islam Salah.
* <p>
* https://github.com/IslamSalah
* islamsalah007@gmail.com
*/
public class CustomLinearLayoutManager extends LinearLayoutManager {
private Context mContext;
private int spanCount;
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
this.mContext = context;
}
@Override
public void measureChild(View child, int widthUsed, int heightUsed) {
RecyclerView.LayoutParams lpTmp = (RecyclerView.LayoutParams) child.getLayoutParams();
final RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(measureScreenWidth(mContext) / spanCount, lpTmp.height);
int widthSpec = View.MeasureSpec.makeMeasureSpec(measureScreenWidth(mContext) / spanCount, View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(lp.height, View.MeasureSpec.EXACTLY);
child.measure(widthSpec, heightSpec);
}
@Override
public void measureChildWithMargins(View child, int widthUsed, int heightUsed) {
RecyclerView.LayoutParams lpTmp = (RecyclerView.LayoutParams) child.getLayoutParams();
final RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(measureScreenWidth(mContext) / spanCount, lpTmp.height);
int widthSpec = View.MeasureSpec.makeMeasureSpec(measureScreenWidth(mContext) / spanCount, View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(lp.height, View.MeasureSpec.EXACTLY);
child.measure(widthSpec, heightSpec);
}
private int measureScreenWidth(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
return point.x;
}
public int getSpanCount() {
return spanCount;
}
public void setSpanCount(int spanCount) {
this.spanCount = spanCount;
}