我想制作一个有两个不同列数的gridview。
实际上,我的gridview有两列fig1
,第五列的列数应为fig2
。
我附上我的屏幕截图以获得更多说明,
// fig1:当前输出
// fig2:最终输出
答案 0 :(得分:3)
尝试使用AsymmetricGridView
我写了一个例子来帮助你解决问题
结果与 fig2 相同,但没有图片
在build.gradle
文件中:
dependencies {
compile 'com.felipecsl.asymmetricgridview:library:2.0.1'
}
在activity_main.xml
文件中:
<com.felipecsl.asymmetricgridview.library.widget.AsymmetricGridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:divider="@android:color/transparent"
android:fadingEdge="none"
android:focusable="false"
android:gravity="center"
android:dividerHeight="3dp"
android:listSelector="#00000000"/>
在DemoAdapter.java
文件中:
import android.widget.ListAdapter;
import com.felipecsl.asymmetricgridview.app.model.DemoItem;
import java.util.List;
public interface DemoAdapter extends ListAdapter {
void appendItems(List<DemoItem> newItems);
void setItems(List<DemoItem> moreItems);
}
在DefaultListAdapter.java
文件中:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.felipecsl.asymmetricgridview.app.R;
import com.felipecsl.asymmetricgridview.app.model.DemoItem;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* Sample adapter implementation extending from AsymmetricGridViewAdapter<DemoItem> This is the
* easiest way to get started.
*/
public class DefaultListAdapter extends ArrayAdapter<DemoItem> implements DemoAdapter {
private final LayoutInflater layoutInflater;
public DefaultListAdapter(Context context, List<DemoItem> items) {
super(context, 0, items);
layoutInflater = LayoutInflater.from(context);
}
public DefaultListAdapter(Context context) {
super(context, 0);
layoutInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, @NotNull ViewGroup parent) {
View v;
DemoItem item = getItem(position);
boolean isRegular = getItemViewType(position) == 0;
if (convertView == null) {
v = layoutInflater.inflate(
isRegular ? R.layout.adapter_item : R.layout.adapter_item_odd, parent, false);
} else {
v = convertView;
}
TextView textView;
if (isRegular) {
textView = (TextView) v.findViewById(R.id.textview);
} else {
textView = (TextView) v.findViewById(R.id.textview_odd);
}
textView.setText(String.valueOf(item.getPosition()));
return v;
}
@Override public int getViewTypeCount() {
return 2;
}
@Override public int getItemViewType(int position) {
return position % 2 == 0 ? 1 : 0;
}
public void appendItems(List<DemoItem> newItems) {
addAll(newItems);
notifyDataSetChanged();
}
public void setItems(List<DemoItem> moreItems) {
clear();
appendItems(moreItems);
}
}
在adapter_item.xml
:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/text_view_background_selector"
android:gravity="center"
android:textColor="#fff"
android:textSize="18sp" />
在adapter_item_odd.xml
文件中:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview_odd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#007bbb"
android:gravity="center"
android:textColor="#0f0"
android:textSize="18sp" />
在DemoItem.java
文件中:
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import com.felipecsl.asymmetricgridview.library.model.AsymmetricItem;
public class DemoItem implements AsymmetricItem {
private int columnSpan;
private int rowSpan;
private int position;
public DemoItem() {
this(1, 1, 0);
}
public DemoItem(int columnSpan, int rowSpan, int position) {
this.columnSpan = columnSpan;
this.rowSpan = rowSpan;
this.position = position;
}
public DemoItem(Parcel in) {
readFromParcel(in);
}
@Override public int getColumnSpan() {
return columnSpan;
}
@Override public int getRowSpan() {
return rowSpan;
}
public int getPosition() {
return position;
}
@Override public String toString() {
return String.format("%s: %sx%s", position, rowSpan, columnSpan);
}
@Override public int describeContents() {
return 0;
}
private void readFromParcel(Parcel in) {
columnSpan = in.readInt();
rowSpan = in.readInt();
position = in.readInt();
}
@Override public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(columnSpan);
dest.writeInt(rowSpan);
dest.writeInt(position);
}
/* Parcelable interface implementation */
public static final Parcelable.Creator<DemoItem> CREATOR = new Parcelable.Creator<DemoItem>() {
@Override public DemoItem createFromParcel(@NonNull Parcel in) {
return new DemoItem(in);
}
@Override @NonNull public DemoItem[] newArray(int size) {
return new DemoItem[size];
}
};
}
在MainActivity.java
文件中:
public class MainActivity extends AppCompatActivity{
private static final String TAG = "MainActivity";
private AsymmetricGridView listView;
private DemoAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (AsymmetricGridView) findViewById(R.id.listView);
if (savedInstanceState == null) {
adapter = new DefaultListAdapter(this, getMoreItems(50));
} else {
adapter = new DefaultListAdapter(this);
}
listView.setRequestedColumnCount(2);
listView.setRequestedHorizontalSpacing(Utils.dpToPx(this, 3));
listView.setAdapter(getNewAdapter());
listView.setDebugging(true);
}
private AsymmetricGridViewAdapter<?> getNewAdapter() {
return new AsymmetricGridViewAdapter<>(this, listView, adapter);
}
private List<DemoItem> getMoreItems(int qty) {
List<DemoItem> items = new ArrayList<>();
DemoItem item = new DemoItem(1, 1, 0);
DemoItem item1 = new DemoItem(1, 1, 1);
DemoItem item2 = new DemoItem(1, 1, 2);
DemoItem item3 = new DemoItem(1, 1, 3);
DemoItem item4 = new DemoItem(2, 2, 4);
DemoItem item5 = new DemoItem(1, 1, 5);
DemoItem item6 = new DemoItem(1, 1, 6);
items.add(item);
items.add(item1);
items.add(item2);
items.add(item3);
items.add(item4);
items.add(item5);
items.add(item6);
return items;
}
}
答案 1 :(得分:2)
您可以使用以下构建UI来执行此操作:构造一个具有垂直方向的ListView(或带有单个列的GridView),并在其中放入一个GridView,前两行有两列,一个ImageView(或其他用于紫色视图)和另一个GridView,最后一行有两列(体育和艺术)。
如果您需要将整行作为单个列Merge rows and columns in android Gridview
,请使用GridLayout并合并列