我已经在gridView中创建了一个自定义gridView和一个项目,但是如何在HomeActivity类文件中设置这些自定义gridView onCreate?
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// how can I say here that I want to set the gridView from list_holder.xml
}
}
list_holder.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/view_item_block_holder"
android:layout_alignParentEnd="false"
android:numColumns="auto_fit"
tools:listitem="@layout/item_block" />
</RelativeLayout>
item_block.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_block"
android:padding="5dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/secondaryBackgroundColor">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/secondaryBackgroundColor"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/imageView2"
android:background="@drawable/no_image_available" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/secondaryBackgroundColor"
android:padding="10dp"
android:layout_margin="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="video title"
android:id="@+id/item_block_title"
android:textColor="@color/textTitleColor"
android:textStyle="bold"
android:textSize="15dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/item_block_description"
android:textColor="@color/textContentColor"
android:textSize="10dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="12-09-16"
android:id="@+id/item_date"
android:textColor="@color/textContentColor"
android:textSize="10dp"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:1)
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//Here Adapter like
AdapterDemoGrid adapterDemoGrid = new AdapterDemoGrid(this);
gridView.setAdapter(adapterDemoGrid);
}
}
这里,Custom GridView的适配器制作这个类:
public class AdapterDemoGrid extends BaseAdapter {
private final Context context;
private LayoutInflater mLayoutInflater;
public AdapterDemoGrid(Context context) {
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
//lastPosition = -1;
}
@Override
public int getCount() {
return 20;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null) {
//The view is not a recycled one: we have to inflate
view = mLayoutInflater.inflate(R.layout.item_block.xml, viewGroup, false);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
return view;
}
class ViewHolder {
/// Item Block views goes here...
}
}
希望它能帮到你!!