我的导航抽屉没有正确反应

时间:2015-02-03 22:24:58

标签: android android-layout android-activity android-listview navigation-drawer

我的导航抽屉有问题。当用户单击一个按钮时,我想更改用户单击的元素的图标。有谁能够帮我。 这是我的导航列表适配器

public class NavigationListAdapter extends ArrayAdapter<NavDrawerItem>{

private LayoutInflater inflater;
private NavMenuPitanjaHolder navMenuPitanjaHolder;

public NavigationListAdapter(Context context, int textViewResourceId,
        ArrayList<NavDrawerItem> objects) {
    super(context, textViewResourceId, objects);
    this.inflater = LayoutInflater.from(context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    NavDrawerItem menuItem = this.getItem(position);
    if (menuItem.getType() == NavigationItem.ITEM_TYPE) {
        view = getItemView(position, convertView, parent, menuItem);
    } 
    else if (menuItem.getType() == NavigationPitanja.PITANJA_TYPE){
        view = getPitanjaView(position, convertView, parent, menuItem);
    }
    else {
        view = getSectionView(position, convertView, parent, menuItem);
    }
    return view;
}

public View getItemView(int position, View convertView, ViewGroup parentView,
        NavDrawerItem navDrawerItem) {

    NavigationItem menuItem = (NavigationItem) navDrawerItem;
    NavMenuItemHolder navMenuItemHolder = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.drawer_list_item, parentView,
                false);
        TextView labelView = (TextView) convertView
                .findViewById(R.id.navmenuitem_label);
        ImageView i = (ImageView) convertView
                .findViewById(R.id.navmenuitem_icon);

        navMenuItemHolder = new NavMenuItemHolder();
        navMenuItemHolder.labelView = labelView;
        navMenuItemHolder.image = i;

        convertView.setTag(navMenuItemHolder);
    }

    if (navMenuItemHolder == null) {
        navMenuItemHolder = (NavMenuItemHolder) convertView.getTag();
    }

    navMenuItemHolder.labelView.setText(menuItem.getLabel().toString());
    navMenuItemHolder.image.setImageResource(menuItem.getIcon());

    return convertView;
}

public View getSectionView(int position, View convertView, ViewGroup parentView,
        NavDrawerItem navDrawerItem) {

    NavigationSection menuSection = (NavigationSection) navDrawerItem;
    NavMenuSectionHolder navMenuItemHolder = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.darwer_list_section,
                parentView, false);
        TextView labelView = (TextView) convertView
                .findViewById(R.id.navmenusection_label);

        navMenuItemHolder = new NavMenuSectionHolder();
        navMenuItemHolder.labelView = labelView;
        convertView.setTag(navMenuItemHolder);
    }

    if (navMenuItemHolder == null) {
        navMenuItemHolder = (NavMenuSectionHolder) convertView.getTag();
    }

    navMenuItemHolder.labelView.setText(menuSection.getLabel().toString());

    return convertView;
}

public View getPitanjaView(int position, View convertView, ViewGroup parentView, NavDrawerItem navDrawerItem){

    NavigationPitanja pitanjaSection = (NavigationPitanja) navDrawerItem;
    navMenuPitanjaHolder = null;
    if (convertView == null){
        convertView = inflater.inflate(R.layout.drawer_list_pitanja, parentView, false);
        TextView naslovView = (TextView) convertView.findViewById(R.id.naslovPitanja);
        TextView textView = (TextView) convertView.findViewById(R.id.textPitanja);
        ImageView i = (ImageView) convertView.findViewById(R.id.tocno_netocno_imageView);

        navMenuPitanjaHolder = new NavMenuPitanjaHolder();
        navMenuPitanjaHolder.naslovPitanja = naslovView;
        navMenuPitanjaHolder.textPitanja = textView;
        navMenuPitanjaHolder.image = i;
        convertView.setTag(navMenuPitanjaHolder);

    }

    if (navMenuPitanjaHolder == null) {
        navMenuPitanjaHolder = (NavMenuPitanjaHolder) convertView.getTag();
    }

    navMenuPitanjaHolder.naslovPitanja.setText(pitanjaSection.getNaslov().toString());
    if (pitanjaSection.getText().toString().length() < 20)
        navMenuPitanjaHolder.textPitanja.setText(pitanjaSection.getText().toString());
    else
        navMenuPitanjaHolder.textPitanja.setText(pitanjaSection.getText().substring(0, 20).toString() + "...");

    return convertView;

}

public void iconized(Pitanja item, int id){
    if (item.getIdPitanja() == id){
        navMenuPitanjaHolder.image.setImageResource(R.drawable.correct);
    }
}

@Override
public int getViewTypeCount() {
    return 3;
}

@Override
public int getItemViewType(int position) {
    return this.getItem(position).getType();
}

@Override
public boolean isEnabled(int position) {
    return getItem(position).isEnabled();
}


private static class NavMenuItemHolder {
    private TextView labelView;
    private ImageView image;
}

private static class NavMenuSectionHolder {
    private TextView labelView;
}

private static class NavMenuPitanjaHolder {
    private TextView naslovPitanja, textPitanja;
    private ImageView image;
}


}

这是我的主要活动

@SuppressLint("UseSparseArrays")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    previous.setOnClickListener(this);
    informacije.setOnClickListener(this);
    odgovori.setOnClickListener(this);
    next.setOnClickListener(this);
    //uputa = (RelativeLayout) findViewById(R.id.relativeLayoutUputa1);

    getAllImages = db.getAllPitanjaImages();
    odgBrojPitanja = new HashMap<Integer, List<Integer>>();

    updateDisplay(broj);

}

@Override
protected void onNavItemSelected(int id) {

    if ( id > 200){
        Toast.makeText(getApplicationContext(), "kliknuo si na pitanje" + id + " toliki je id", Toast.LENGTH_SHORT).show();
        updateDisplay(id-200-1);

    } else {
        Toast.makeText(getApplicationContext(), "kliknuo si na izbornik" + id, Toast.LENGTH_SHORT).show();
    }

}

@Override
protected NavDrawerActivityConfiguration getNavDrawerConfiguration() {

    intent = getIntent();
    getAllPitanja = db
            .getAllPitanja(intent.getStringExtra("id_kategorije"));

    m = new ArrayList<NavDrawerItem>();
    m.add(NavigationItem.create(100, "Profil", R.drawable.ikona_profil_bez_okvira_01_01, this));
    m.add(NavigationItem.create(200, "Teme", R.drawable.ikona_kategorije_bez_okvira_01_01,this));
    m.add(NavigationItem.create(300, "Ispiti", R.drawable.ikona_ispit_bez_okvira_01, this));
    m.add(NavigationItem.create(400, "Znakovi", R.drawable.ikona_znakovi_bez_okvira_01, this));
    m.add(new NavigationSection(200, "PITANJA"));
    for (int i=0;i<getAllPitanja.size();i++){
        m.add(NavigationPitanja.create((200+i+1), "Pitanje " + (i+1), getAllPitanja.get(i).getTextPitanja(), this));
    }


    NavDrawerActivityConfiguration navDrawerActivityCOnfiguration = new NavDrawerActivityConfiguration();
    navDrawerActivityCOnfiguration.setLayout(R.layout.activity_pitanje);
    navDrawerActivityCOnfiguration.setDrawerLayoutId(R.id.drawer_layout_pitanja);
    navDrawerActivityCOnfiguration.setLeftDrawer(R.id.listaDesnoPitanja);
    navDrawerActivityCOnfiguration.setNavigationItem(m);
    adapte = new NavigationListAdapter(this, R.layout.drawer_list_item, m);
    navDrawerActivityCOnfiguration.setBaseAdapter(adapte);
    return navDrawerActivityCOnfiguration;

}

@SuppressLint("SdCardPath")
public void updateDisplay(int z) {

    odgovorList = db.getAllOdgovore(Integer.toString(getAllPitanja.get(z)
            .getIdPitanja()));
    adapter = new PitanjaAdapter(this,
            R.layout.pitanja_entry, odgovorList);
    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);

}

@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.previousButton:
            broj--;
            if (broj < 0) {
                broj = getAllPitanja.size() - 1;
            }
            updateDisplay(broj);
        break;
    case R.id.odgovoriButton:
        postaviPitanje(broj);
        break;
    case R.id.nextButton:           
            broj++;
            if (broj > (getAllPitanja.size() - 1)) {
                broj = 0;
            }
            updateDisplay(broj);
        break;

    }
}

private void postaviPitanje(int z) {

    for (int i = 0; i < odgovorList.size(); i++) {
        Odgovor odgovor = odgovorList.get(i);
        if (Integer.parseInt(odgovor.getIs_correct()) == 0
                && odgovor.isSelected()
                || Integer.parseInt(odgovor.getIs_correct()) == 1
                && !odgovor.isSelected()) {
            tocno = false;
        }

    }
    if (tocno){
        getAllPitanja.get(z).setTocno(true);
        Toast.makeText(getApplicationContext(), "Tocno ste odgovorili na pitanje cij je id " + getAllPitanja.get(z).getIdPitanja() + 200, Toast.LENGTH_SHORT).show();
        Log.i("Sljedeci mjenjeam", String.valueOf(200+getAllPitanja.get(z).getIdPitanja()));
        adapte.iconized(getAllPitanja.get(z), getAllPitanja.get(z).getIdPitanja());
        Log.i("Sljedeci item sam iconizirao", getAllPitanja.get(z).getTextPitanja());
    }
    else {
        getAllPitanja.get(z).setNetocno(true);
        Toast.makeText(getApplicationContext(), "Niste tocno odgovorili", Toast.LENGTH_SHORT).show();
    }

}

}

任何人都知道为什么我的适配器中的公共方法会在抽屉布局中每隔七张图片变换一次。如果我有37个项目。每七个项目图标都会更改。任何人都有解决方案。你能帮我吗?

1 个答案:

答案 0 :(得分:1)

问题是您在适配器中使用了navMenuPitanjaHolder的单个实例,这通常是一种不好的做法。您尝试实现的ViewHolder模式的想法是为每个不同的convertView创建一个ViewHolder类的实例(在您的情况下为NavMenuPitanjaHolder),但您在getPitanjaView()中没有这样做。

通过iconized()设置图片后,您正在为navMenuPitanjaHolder设置图片,因此使用该图片的所有视图都会获得图片。它是每个X项,因为Android正在尝试尽可能多地重用视图。简而言之 - 这种方法在概念上是错误的,您应该删除它。

更好的方法是在NavigationPitanja类中添加一个变量,如果答案正确或错误,它将保留信息。然后在getPitanjaView()中你会有类似的东西:

if (pitanjaSection.isAnswerCorrect()) {
    navMenuPitanjaHolder.image.setImageDrawable(R.drawable.correct);
} else {
    navMenuPitanjaHolder.image.setImageDrawable(R.drawable.false);
}

希望这能为您找到解决问题的正确方向!