我想使用listview和一个扩展BaseAdapter的自定义适配器
public class PowerAdapter extends BaseAdapter {
List<Power> mPowerList;
public PowerAdapter(List<Power> powers) {
mPowerList = powers;
}
public void bind(ViewHolder holder, int position) {
Power power = mPowerList.get(position);
Context context = holder.mContext;
holder.mPowerName.setText(power.getName());
holder.mPowerLevel.setText(context.getString(R.string.power_level, power.getMinLevel()));
holder.mPowerCost.setText(context.getString(R.string.power_cost, power.getCost()));
holder.mPowerDuration.setText(context.getString(R.string.power_duration, power.getDuration()));
holder.mPowerCooldown.setText(context.getString(R.string.power_cooldown, power.getCoolDown()));
}
@Override
public int getCount() {
return mPowerList.size();
}
@Override
public Object getItem(int position) {
return mPowerList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_power, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
bind(viewHolder, position);
return convertView;
}
public static class ViewHolder {
@Bind(R.id.power_name) TextView mPowerName;
@Bind(R.id.power_level) TextView mPowerLevel;
@Bind(R.id.power_cost) TextView mPowerCost;
@Bind(R.id.power_duration) TextView mPowerDuration;
@Bind(R.id.power_cooldown) TextView mPowerCooldown;
Context mContext;
public ViewHolder(View itemView) {
ButterKnife.bind(this, itemView);
mContext = itemView.getContext();
}
}}
修改
Log.d("SIZE", Integer.toString(section.getPowerList().size())); // return 3
holder.mCharacteristics.setAdapter(new PowerAdapter(section.getPowerList()));
和布局: 活动:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<include layout="@layout/widget_toolbar"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/hero_model"
android:layout_width="match_parent"
android:layout_height="wrap_content" /></LinearLayout>
以及每个Recycler项目的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/section_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="16dp" />
<ListView
android:id="@+id/charac_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/></LinearLayout>
编辑2 Okey,它不起作用,因为我的ListView具有“wrap_content”大小,如果我用“200dp”修复大小,它可以工作。 但我想要动态尺寸。有人知道怎么修这个东西吗 ?
答案 0 :(得分:3)
我认为你不应该在RecyclerView中使用Listview。您不能对ListView使用wrap_content(它将缩小到0高度)。 所以,让我们使用StickyRecyclerView或类似的东西。请参考这个例子
https://gist.github.com/gabrielemariotti/e81e126227f8a4bb339c