Gridview展开代码 [英] Gridview Expand code

查看:108
本文介绍了Gridview展开代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,在其中我要将图像设置到gridview上,并在itemclick上我想通过textview显示图像的描述.我已经将图像设置到gridview上,但是现在我想做第二部分.

I am making an app in which I want to set images onto gridview and on itemclick I want to show the description of an image via textview. I have already done the setting of images onto gridview, but now I want to do the second part of it.

以下图像显示了布局的可视化:

Following Images shows the visualization of The layout :

  • 之前:

  • Before :

之后:

基本上,我想在单击的图像下方的2行gridview之间添加新的布局.我做了很多研究,还看到了许多库和StackOverflow答案,但不知道这样做.我需要示例代码才能对gridview或其他任何视图执行此操作.谢谢.

Basically, I want to Add a new layout in between of 2 rows of gridview below the clicked image. I have done a lot of research and also seen many libraries and StackOverflow answers but can't figure out to do so. I need example code to do so for gridview or any other views. Thanks.

我经历过的所有stackoverflow答案和库:=

All the stackoverflow Answers and libraries that i have gone through :=

  • Android expandable GridView like Google images
  • Add view between 2 rows Gridview
  • Dynamically adding a whole row in GridView, or splitting the View

库->

https://github.com/thewaychung/ExpandableGridView

推荐答案

您可以尝试以下方法供您参考: 我正在使用文本视图而不是ImageView.但是概念保持不变. 显示的内容是这样的.

You can try the following approach for your reference : I am using text view instead of ImageView. But the concept remain same. It is displayed something like this.

Const.java:保持常量的类.

Const.java : Class to keep constants.

public class Const {
public static final int DES_VIEW=0;
public static final int FOOD_VIEW=1;
public static final int FAKE_VIEW=2;
public static final int COLUMN_NUMBER = 2;
}

RowData.java:一个接口,适配器类将使用该接口来获取每个元素的视图类型.

RowData.java : An Interface which will be used by the adapter class to get view type of each element.

public interface RowData {
int getViewType();
}

FoodItem.java:只是一个简单的pojo类,它实现RowData(已填充父项FoodItem item 0)

FoodItem.java : Just a plain pojo class which implements RowData ( The parent item FoodItem item 0 is populated )

public class FoodItem implements RowData{
public int getViewType() {
    return viewType;
}

public void setViewType(int viewType) {
    this.viewType = viewType;
}

private int viewType;
public boolean isExpanded() {
    return isExpanded;
}

public void setExpanded(boolean expanded) {
    isExpanded = expanded;
}

private boolean isExpanded;

public String getFoodName() {
    return foodName;
}

public void setFoodName(String foodName) {
    this.foodName = foodName;
}

private String foodName;

public String getDescription() {
    return description == null ? "DEFAULT" : description;
}

public void setDescription(String description) {
    this.description = description;
}

private String description;

}

FakeData.java:Pojo类,用于填充空视图.

FakeData.java : Pojo class to populate empty views.

public class FakeData implements RowData {
@Override
public int getViewType() {
    return Const.FAKE_VIEW;
}
}

FoodAdapter.java:适配器类.

FoodAdapter.java : Adapter class.

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

public void setFoodItems(List<RowData> foodItems) {
    this.foodItems = foodItems;
   // addBlankViews();
}



private List<RowData> foodItems;

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;

    switch (viewType) {
        case Const.FOOD_VIEW:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, parent, false);
            return new VH(view);
        case Const.DES_VIEW:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.des, parent, false);
            return new DesView(view);

        case Const.FAKE_VIEW:
            Log.e("CALLED", " ---> " + viewType);
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, parent, false);
            return new FakeView(view);
            default:
                return null;
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    int view = getItemViewType(position);

    switch (view) {
        case Const.FAKE_VIEW: {
            FakeView fakeView = (FakeView) holder;
            Log.e("CALLED", position + " " + view);
        }
        break;

        case Const.DES_VIEW: {
            DesView desView = (DesView) holder;
            desView.setData((FoodItem) getFoodItem(position));
        }
        break;
        case Const.FOOD_VIEW: {
            VH foodView = (VH) holder;
            foodView.setData((FoodItem) getFoodItem(position));

        }
        break;

    }

}

RowData getFoodItem(int position) {
    if (foodItems != null) {
        return foodItems.get(position);
    }
    return null;
}

@Override
public int getItemViewType(int position) {
    RowData foodItem = getFoodItem(position);
    if (foodItem != null) {
        return foodItem.getViewType();

    }
    return -1;
}

@Override
public int getItemCount() {
    if (foodItems == null) {
        return 0;
    } else {
        return foodItems.size();
    }

}


class VH extends RecyclerView.ViewHolder {
    TextView foodItemName;


    public VH(View itemView) {
        super(itemView);
        foodItemName = itemView.findViewById(R.id.food_item);
        foodItemName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FoodAdapter.this.onClick(getAdapterPosition());

            }
        });
    }


    void setData(FoodItem data) {
        foodItemName.setText(data.getFoodName());
    }
}

class DesView extends RecyclerView.ViewHolder {
    TextView foodDesView;


    public DesView(View itemView) {
        super(itemView);
        foodDesView = itemView.findViewById(R.id.des_item);
        foodDesView.setTextColor(Color.BLUE);
    }


    void setData(FoodItem data) {
        foodDesView.setText(data.getDescription());
    }
}

class FakeView extends RecyclerView.ViewHolder {

    public FakeView(View itemView) {
        super(itemView);

    }
}

private int currentExpandedIndex = -1;

private void onClick(int position) {
 //  calculation to show and hide the child view (with magenta background ).
    if (currentExpandedIndex != -1) {
        foodItems.remove(currentExpandedIndex);
        currentExpandedIndex = -1;
        notifyDataSetChanged();
        return;
    }
    int startIndex = position % Const.COLUMN_NUMBER;
    if (startIndex == 0) {
        startIndex = position;
    } else {
        startIndex = position - startIndex;
    }
    startIndex = startIndex + Const.COLUMN_NUMBER;

    if (currentExpandedIndex == -1) {
        FoodItem des = new FoodItem();
        des.setViewType(Const.DES_VIEW);
        FoodItem foodItem = (FoodItem) getFoodItem(position);
        des.setDescription(foodItem.getDescription());
        foodItems.add(startIndex, des);
        currentExpandedIndex = startIndex;
    }
    notifyDataSetChanged();
}
}

MainActivity.java:活动类.

MainActivity.java : Activity class.

 public class MainActivity extends AppCompatActivity {
private FoodAdapter foodAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RecyclerView recyclerView = findViewById(R.id.reycleView);
    ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

    foodAdapter = new FoodAdapter();
    foodAdapter.setFoodItems(getFoodItems());
    GridLayoutManager mLayoutManager = new GridLayoutManager(this,Const.COLUMN_NUMBER);
    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            switch (foodAdapter.getItemViewType(position)) {
                case Const.DES_VIEW:
                    return Const.COLUMN_NUMBER;
                default:
                    return 1;
            }


        }
    });
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(foodAdapter);


}

private List<RowData> foodItems;

private List<RowData> getFoodItems() {
    foodItems = new ArrayList<>();
    for (int i = 0; i < 18; i++) {
        FoodItem foodItem = new FoodItem();
        foodItem.setFoodName(" FoodItem item " + i);
        foodItem.setExpanded(false);
        foodItem.setDescription("Fake des of " + i);
        foodItem.setViewType(Const.FOOD_VIEW);
        foodItems.add(foodItem);
    }
    // calculation to add empty view.
    // these view will be added when, there is shortage of parent views to   fill row completely  

    int size = foodItems.size();
    int blankView = size % Const.COLUMN_NUMBER;
    if (blankView != 0) {
        blankView = Const.COLUMN_NUMBER - blankView;
        for (int i = 0; i < blankView; i++) {
            foodItems.add(new FakeData());
        }
    }

    return foodItems;

}


}

activity_main.xml

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="check.service.com.genericrecylerview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/reycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

des.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@color/colorAccent"
android:layout_height="wrap_content">
 <TextView
android:id="@+id/des_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

layout.xml

layout.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="@+id/food_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

这篇关于Gridview展开代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆