gpt4 book ai didi

java - 实现自定义 Android GridView

转载 作者:行者123 更新时间:2023-11-30 01:33:34 25 4
gpt4 key购买 nike

我已经制作了一个自定义 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>

最佳答案

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);

}
}

Here, Adapter for Custom GridView Make this class :

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...

}
}

希望对您有所帮助!!

关于java - 实现自定义 Android GridView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35407098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com