具有不同列长和空白列的网格布局

时间:2019-05-07 21:52:37

标签: java android xml gridview android-recyclerview

我正在尝试创建一个附加的网格布局。我希望有一个网格,其中各列的长度不同,并且如果还有一些空间,则应在布局中显示一个空白矩形。

我尝试了交错网格视图,但没有得到所需的结果。

网格项

<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:background="@drawable/rounded_grey_layout"
android:layout_margin="@dimen/_2sdp"
android:orientation="vertical">


<TextView
    android:id="@+id/individual_grid_item_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/_8sdp"
    android:textAlignment="center"
    android:textColor="@color/text_color"
    android:textSize="@dimen/_12sdp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


 </androidx.constraintlayout.widget.ConstraintLayout>

适配器

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

private static final String TAG = "StaggeredRecyclerViewAd";

private Context mContext;
private profile_view_activity prof_activity;

ArrayList<individual_interest_item> default_interests_list;
String[] default_interests_array = {"Sports","Travel","Photography","Watching movies","Fitness","Art","Nightlife","Nature","Gaming","Culture","Food","Technology","Music","Meeting new people","Relationship","Cars","Friendship"};
Animation animation;


public StaggeredRecyclerViewAdapter(Context context, profile_view_activity pro_activity) {
    this.prof_activity = pro_activity;
    default_interests_list = new ArrayList<>();
    for (String item : default_interests_array)
    {
        individual_interest_item individual_interest_item = new individual_interest_item(item,false);
        default_interests_list.add(individual_interest_item);

    }
    mContext = context;
    animation = AnimationUtils.loadAnimation(mContext.getApplicationContext(), R.anim.alpha);
}

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

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    Log.d(TAG, "onBindViewHolder: called.");

    holder.name.setText(default_interests_list.get(position).interest_name);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (default_interests_list.get(position).is_selected)
            {
                default_interests_list.get(position).is_selected = false;
            }else{
                default_interests_list.get(position).is_selected = true;
            }

            notifyDataSetChanged();
        }
    });
    if (default_interests_list.get(position).is_selected) {
        holder.itemView.setBackground(mContext.getResources().getDrawable(R.drawable.new_rounded_background_buttons_border));
        holder.name.setTextColor(mContext.getResources().getColor(R.color.white));
    }else{
         holder.itemView.setBackground(mContext.getResources().getDrawable(R.drawable.rounded_grey_layout));
         holder.name.setTextColor(mContext.getResources().getColor(R.color.text_color));
    }


}

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

public class ViewHolder extends RecyclerView.ViewHolder{

    ImageView image;
    TextView name;

    public ViewHolder(View itemView) {
        super(itemView);
        this.name = itemView.findViewById(R.id.individual_grid_item_text_view);
    }
}

}

列的高度应该是恒定的,宽度可以是可变的,中间有空白列。enter image description here

0 个答案:

没有答案