视图在列表视图中重复多次

时间:2015-12-10 13:41:32

标签: java android navigation-drawer baseadapter

嗨伙计们,我在导航抽屉的列表视图中获取多个文本,我只需要一次这个文本,而在男性类别中的简单平凡操作下面,但是我在列表视图中的每个文本下面

Navgation_adapter.class

public class Navgation_adapter extends BaseAdapter {
    private Context context;
    private List<Data> catdata;
    private LayoutInflater inflater;

    public Navgation_adapter(Context context, List<Data> catdata) {
        this.context = context;
        this.catdata = catdata;
    }

    @Override
    public int getCount() {
        return catdata.size();
    }

    @Override
    public Object getItem(int i) {
        return catdata.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder holder;
        if (inflater == null)
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.navigation_list, viewGroup, false);
            holder.categories = (TextView) view.findViewById(R.id.categories);
            holder.subcategories = (TextView) view.findViewById(R.id.subcategories);
            holder.whole = (TextView) view.findViewById(R.id.whole);
            holder.catline=view.findViewById(R.id.catline);
            holder.catline2=view.findViewById(R.id.catline2);
            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
        }
        Data ndata = catdata.get(i);
        if (ndata.getCatergory() == null) {
            holder.categories.setText(ndata.getCatergory());
            holder.subcategories.setText(ndata.getSubcatergory());
            holder.categories.setVisibility(View.GONE);
            holder.catline.setVisibility(View.GONE);
            holder.catline2.setVisibility(View.VISIBLE);
            holder.subcategories.setVisibility(View.VISIBLE);
            holder.whole.setVisibility(View.GONE);
            holder.whole.setVisibility(View.VISIBLE);
        } else {
            holder.categories.setText(ndata.getCatergory());
            holder.subcategories.setText(ndata.getSubcatergory());
            holder.categories.setVisibility(View.VISIBLE);
            holder.catline.setVisibility(View.VISIBLE);
            holder.catline2.setVisibility(View.GONE);
            holder.subcategories.setVisibility(View.GONE);
            holder.whole.setVisibility(View.GONE);
        }

        return view;
    }

    static class ViewHolder {
        TextView categories, subcategories,whole;
        View catline,catline2;
    }
}[![Image of Navigation drawer][1]][1]

1 个答案:

答案 0 :(得分:0)

public Navgation_adapter(Context context, List<Data> catdata) {
    this.context = context;
    this.catdata = catdata;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

从onCreateView中删除

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    ViewHolder holder;

//在inflater中删除它

 if (inflater == null)
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)

修改

 private class DrawerAdapter extends BaseAdapter {

    private LayoutInflater inflater = null;

    public DrawerAdapter() {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return aDrawerTitle.length;
    }

    @Override
    public String getItem(int position) {
        return aDrawerTitle[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_lv_drawer, null);

            holder = new ViewHolder();
            holder.ll_item_drawer = (LinearLayout) convertView.findViewById(R.id.ll_item_drawer);

            holder.tv_drawer_item_title = (TextView) convertView.findViewById(R.id.tv_item_drawer);
            holder.tv_item_drawer_icon = (TextView) convertView.findViewById(R.id.tv_item_drawer_icon);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        if (mDrawerList.isItemChecked(position)) {
            holder.ll_item_drawer.setBackgroundColor(context.getResources().getColor(R.color.material_grey_100));
        } else {
            holder.ll_item_drawer.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
        }


        holder.tv_drawer_item_title.setText(getItem(position));
        holder.tv_item_drawer_icon.setText(aDrawerIcon[position]);

        Common.setAwesomeFont(context, holder.tv_item_drawer_icon);

        return convertView;
    }

    private class ViewHolder {
        LinearLayout ll_item_drawer = null;
        TextView tv_drawer_item_title = null, tv_item_drawer_icon = null;
    }

}

并设置值字符串(string.xml)

在全球声明变量

    private String[] aDrawerTitle = null, aDrawerIcon = null;

获取所有数据

    aDrawerTitle = getResources().getStringArray(R.array.app_menu);
    aDrawerIcon = getResources().getStringArray(R.array.app_menu_icon);

在string.xml中

<!-- Menu -->
<string-array name="app_menu">
    <item>Item - 1</item>
    <item>Item - 2</item>
    <item>Item - 3</item>
    <item>Item - 4</item>
    <item>Item - 5</item>
    <item>Item - 6</item>
</string-array>

<string-array name="app_menu_icon">
    <item>@string/fa_comments_o</item>
    <item>@string/fa_envelope_o</item>
    <item>@string/fa_globe</item>
    <item>@string/fa_calendar</item>
    <item>@string/fa_cog</item>
    <item>@string/fa_sign_out</item>
</string-array>