我正在创建一个应用程序,我想将图像设置到gridview和itemclick上,我想通过textview显示图像的描述。我已经在gridview上完成了图像设置,但现在我想做第二部分。
以下图片显示了布局的可视化:
基本上,我想在点击图像下方的2行gridview之间添加新布局。我做了很多研究,并且看到了很多库和StackOverflow的答案,但却无法做到这一点。我需要示例代码来为gridview或任何其他视图执行此操作。谢谢。
我经历过的所有stackoverflow Answers和库:=
答案 0 :(得分:1)
您可以尝试以下方法供您参考: 我使用的是文本视图而不是ImageView。但这个概念仍然相同。 它显示的是这样的。
Const.java:保持常量的类。
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:适配器类将使用的接口,用于获取每个元素的视图类型。
public interface RowData {
int getViewType();
}
FoodItem.java:只是一个实现RowData的普通pojo类(填充父项FoodItem项0)
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类。
public class FakeData implements RowData {
@Override
public int getViewType() {
return Const.FAKE_VIEW;
}
}
FoodAdapter.java:适配器类。
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:活动类。
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中
<?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
<?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>