我试图找出如何将动态ListView实现为片段,但无法弄明白。该项目使用自定义ArrayAdapter。
Custom ListView with custom adapter
我想我会链接教程页面而不是仅仅粘贴一切。我将如何在片段页面中进行此工作?
FeaturedDealsFragment.java(片段类)
public class FeaturedDealsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment_featured_deals, container, false);
ListView LV = (ListView) this.getActivity().findViewById(R.id.listView1);
Deal deal_data[] = new Deal[]
{
new Deal("Deal 1", R.drawable.scenery, "500/100 Supporters", "$500"),
new Deal("Deal 2", R.drawable.scenery, "500/100 Supporters", "$500"),
new Deal("Deal 3", R.drawable.scenery, "500/100 Supporters", "$500"),
new Deal("Deal 4", R.drawable.scenery, "500/100 Supporters", "$500"),
new Deal("Deal 5", R.drawable.scenery, "500/100 Supporters", "$500")
};
DealAdapter adapter = new DealAdapter(getActivity(), R.layout.listview_item_row, deal_data);
View header = (View)inflater.inflate(R.layout.listview_header_row, null);
LV.addHeaderView(header);
LV.setAdapter(adapter);
return V;
}
}
DealAdapter.java
public class DealAdapter extends ArrayAdapter<Deal>{
Context context;
int layoutResourceId;
Deal data[] = null;
public DealAdapter(Context context, int layoutResourceId, Deal[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
DealHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new DealHolder();
holder.txtDeal = (TextView)row.findViewById(R.id.txtDeal);
holder.imgDealImage = (ImageView)row.findViewById(R.id.imgDealImage);
holder.txtSupporters = (TextView)row.findViewById(R.id.txtSupporters);
holder.txtPrice = (TextView)row.findViewById(R.id.txtPrice);
row.setTag(holder);
}
else
{
holder = (DealHolder)row.getTag();
}
Deal deal = data[position];
holder.txtDeal.setText(deal.deal);
holder.imgDealImage.setImageResource(deal.image);
holder.txtSupporters.setText(deal.supporters);
holder.txtPrice.setText(deal.price);
return row;
}
static class DealHolder
{
TextView txtDeal;
ImageView imgDealImage;
TextView txtSupporters;
TextView txtPrice;
}
}
Deal.java
public class Deal {
public String deal;
public int image;
public String supporters;
public String price;
public Deal() {
super();
}
public Deal(String deal, int image, String supporters, String price) {
super();
this.deal = deal;
this.image = image;
this.supporters = supporters;
this.price = price;
}
}
答案 0 :(得分:1)
只需使用传递给onCreateView方法的LayoutInflater,而不是调用getLayoutInflater()这是Activity类中的方法:
View header = (View) inflater.inflate(R.layout.listview_header_row, null);
Fragment不是Context的子类。获取对Context对象的引用的最简单方法通常是获取对Context的子类的Activity的引用。因此,您需要将WeatherAdapter更改为:
WeatherAdapter adapter = new WeatherAdapter(getActivity(), R.layout.listview_item_row, weather_data);
答案 1 :(得分:0)
我想你想要一个包含一个包含ListView的片段的Activity。
正确的方法是创建一个Fragment并在其布局中添加一个ListView。在其onCreateView方法中获取ListView引用并设置适配器。
在活动中,您可以通过XML布局或以编程方式在onCreate方法中添加Fragment。
我不知道这是不是你想要的。