RecyclerView Android动态高度

时间:2018-10-08 11:22:24

标签: java android android-recyclerview android-linearlayout

我正在寻找在RecyclerView中创建动态高度以在所有设备上响应的方式。目前,这很好用,但是对于开始的前两行,我有一个双单元格,并且这两行的高度设置为第一个单元格。相反,我希望第一行和第二行上的此单元格都与最后一个单元格的高度匹配,并成为一个矩形。我已经尝试了几种方法,但似乎都没有用。我不确定问题出在哪里,但我希望有人可以告诉我。我将在下面附上屏幕截图和代码。

SquareLayout.java

public class SquareLayout extends LinearLayout {

    public SquareLayout(Context context) {
        super(context);
    }

    public SquareLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int width, int height) {
       // int width = MeasureSpec.getSize(widthMeasureSpec);
        //int height = MeasureSpec.getSize(heightMeasureSpec);
        // note we are applying the width value as the height
        //if(width>=370){
        //    super.onMeasure(widthMeasureSpec, widthMeasureSpec/2);
       // }
       // else{
        //    super.onMeasure(widthMeasureSpec, widthMeasureSpec);
      //  }

        super.onMeasure(width, width);

       // Log.d("int","int is: "+ width);

    }

}

DataAdapter.java

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private ArrayList<AndroidVersion> android;
    private Context context;


    public DataAdapter(Context context,ArrayList<AndroidVersion> android) {
        this.android = android;
        this.context = context;
    }

    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
//        int width = viewHolder.tv_android.getMeasuredWidth() / 2; //returns -1
        Log.d("MYINT", "value: " + i);
        //Picasso.with(context).load(android.get(i).getAndroid_image_url()).resize(240, 120).into(viewHolder.img_android);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout testheight;
        private ImageView img_android;
        public ViewHolder(View view) {
            super(view);

        }
    }

}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private final String android_version_names[] = {
            "test1",
            "test2",
            "test3",
            "test4",
            "test5",
            "test6",
            "test7",
            "test8",
            "test9",
            "test10"
    };

    private final String android_image_urls[] = {
            "http://example.com/images/test.png",
    };

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


        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(),3);


        ArrayList<AndroidVersion> androidVersions = prepareData();
        ((GridLayoutManager) mLayoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0 || position == 2) {

                    return 2; // ITEMS AT POSITION 1 AND 6 OCCUPY 3 SPACES
                }

                else {
                    return 1; // OTHER ITEMS OCCUPY ONLY A SINGLE SPACE
                }
            }
        });
        recyclerView.setLayoutManager(mLayoutManager);
        DataAdapter adapter = new DataAdapter(getApplicationContext(), androidVersions);
        recyclerView.setAdapter(adapter);

    }
    private ArrayList<AndroidVersion> prepareData(){

        ArrayList<AndroidVersion> android_version = new ArrayList<>();
        for(int i=0;i<android_version_names.length;i++){
            AndroidVersion androidVersion = new AndroidVersion();
            androidVersion.setAndroid_version_name(android_version_names[i]);
            androidVersion.setAndroid_image_url(android_image_urls[i]);
            android_version.add(androidVersion);
        }
        return android_version;
    }
}

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<example.SquareLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@android:color/transparent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="10dp"
        android:background="@color/reloadedpurple"
        android:elevation="6dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img_android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true" />

        <TextView
            android:id="@+id/tv_android"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:textColor="#FFFFFF"
            android:textStyle="bold" />
    </LinearLayout>
</example.SquareLayout>

0 个答案:

没有答案