Android ListView后退按钮

时间:2016-06-06 14:52:16

标签: android listview

我制作了一个列表视图,当我点击一个项目时,它会过滤并显示按我点击的内容过滤的项目。我们可以认为它就像树视图。 我的问题是,当我来到最后一项时“我怎么能回到这个列表视图中的一步呢?”。当我点击设备的后退按钮时,它返回主要活动但我只想返回此列表视图。

OsbItemsListActivity.java

public class OsbItemsListActivity extends AppCompatActivity {
    private ListView sobItemsListView;
    private Activity mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_osb_items_list);
        mContext = this;
        Intent intent = getIntent();
        if(null != intent){
            sobItemsListView = (ListView)findViewById(R.id.activity_osb_items_list_view);
            SobItemsAdapter sobItemsAdapter = new SobItemsAdapter(mContext);
            sobItemsListView.setAdapter(sobItemsAdapter);
            sobItemsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String clickedItem = (String) parent.getAdapter().getItem(position);
                    Toast.makeText(mContext, "SOB : " + clickedItem, Toast.LENGTH_SHORT).show();
                    ((SobItemsAdapter)parent.getAdapter()).setNextFilter(clickedItem);
                    ((SobItemsAdapter) parent.getAdapter()).notifyDataSetChanged();
                }
            });
        }
        else {
            Toast.makeText(mContext, "Listelenecek Veri Yok", Toast.LENGTH_SHORT).show();
        }
    }

}

我的SobItemsAdapter.java

public class SobItemsAdapter extends BaseAdapter {
    public enum STEPS{ SOB, STREET, CONNECTIONARTICLE, INSTALLATION }
    Activity mActivity;
    List<OsbDownloadItem> osbDownloadItemList;
    List<String> visualRows = new ArrayList<String>();
    HashMap<String, List<OsbDownloadItem>> map;
    STEPS filterStep;
    public SobItemsAdapter(Activity activity){
        mActivity = activity;
        filterStep = STEPS.SOB;
        osbDownloadItemList = SharedData.osbDownloadItemList;
        setNextFilter("");
    }
    private void modifyVisualRows(HashMap<String, List<OsbDownloadItem>> _map){
        visualRows.clear();
        for(String item : _map.keySet()){
            visualRows.add(item);
        }
    }
    @Override
    public int getCount() {
        return visualRows.size();
    }
    @Override
    public String getItem(int position) {
        return visualRows.get(position);
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = mActivity.getLayoutInflater();
        View view = inflater.inflate(R.layout.activity_osb_items_row, parent, false);
        if(null != view){
            ((TextView)view.findViewById(R.id.osb_items_sob)).setText(visualRows.get(position));
        }
        return view;
    }
    public void setNextFilter(String value){
        switch (filterStep){
            case SOB:
                map = DataFilter.getSobs(osbDownloadItemList);
                filterStep = STEPS.STREET;
                break;
            case STREET:
                map = DataFilter.getStreetsBySob(map.get(value));
                filterStep = STEPS.CONNECTIONARTICLE;
                break;
            case CONNECTIONARTICLE:
                map = DataFilter.getConnectionArticlesByStreet(map.get(value));
                filterStep = STEPS.INSTALLATION;
                break;
            case INSTALLATION:
                map = DataFilter.getInstalliationsByConnectionArticle(map.get(value));
                break;
        }
        modifyVisualRows(map);
    }
}

1 个答案:

答案 0 :(得分:0)

将以下代码放入您的活动中

    @Override
     public void onBackPressed()
    {
    if(checkBackPress)
     {
    checkBackPress =false;
    sobItemsAdapter.setNextFilter(STEPS.SOB);
     sobItemsAdapter.notifyDataSetChanged();
    }
    super.onBackPressed();  
}

创建一个布尔字段public boolean checkBackPress = false;在你的活动中。一旦项目点击你的项目点击事件(checkBackPress = true)并将checkBackPress设置为反向隐藏

,将此checkBackPress设置为true