在ArrayAdapter中单击项目时创建新片段

时间:2014-02-20 05:44:24

标签: android listview android-fragments android-arrayadapter

我有一个使用ArrayAdapter填充的ListView。我有一个OnItemClickListener附加到这个ListView,它工作正常。现在,当单击一个项目时,我想通过显示一个新片段来切换屏幕视图。我该怎么做呢? 以下是我自定义ArrayAdapter的一些代码:

protected class PostAdapter extends ArrayAdapter<Post> implements OnScrollListener, OnItemClickListener {
        private int previousTotal = 0;
        private boolean loading = true;

        public PostAdapter(List<Post> posts){
            super(getActivity(), R.layout.post_item, posts);
        }   

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = getActivity().getLayoutInflater().inflate(R.layout.post_item, null);
            }

            Post thePost = getItem(position);

            TextView postTitleTextView = (TextView) convertView.findViewById(R.id.post_title);
            postTitleTextView.setText(thePost.getTitle());

            TextView postDetailsTextView = (TextView) convertView.findViewById(R.id.post_details);
            postDetailsTextView.setText(thePost.getDetails());

            TextView postScoreTextView = (TextView) convertView.findViewById(R.id.post_score);
            postScoreTextView.setText(thePost.getScore());

            return convertView;
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {            
            if (loading) {
                //update previous total, adding in the footer 
                if (totalItemCount > previousTotal + 1) {
                    loading = false;
                    previousTotal = totalItemCount; 
                    Log.d("running", "item count: " + totalItemCount);
                }
            }           
            if (!loading && 
                    (firstVisibleItem + visibleItemCount >= totalItemCount)
                    ) {
                new AddPostsTask().execute();
                loading = true;
            }           
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        /**
         * An AsyncTask which takes care of grabbing more posts and updating our 
         * adapter, making sure it is on the UI Thread.
         * @author Sukh
         *
         */
        private class AddPostsTask extends AsyncTask<Void, Void, Void> {    
            @Override
            protected void onPreExecute() {
                postsList.addFooterView(footer);
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {
                Log.i("running", "start fetchMorePosts");               
                posts.addAll(postsHolder.fetchMorePosts());
                Log.i("running", "end fetchMorePosts");
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                Log.i("running", "start adapterChange");
                postsList.removeFooterView(footer);
                notifyDataSetChanged();
                Log.i("running", "end adapterChange");
                super.onPostExecute(result);
            }

        }

        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position,
                long arg3) {
            Post selectedPost = (Post) adapter.getItemAtPosition(position);
            String title = selectedPost.getTitle();
            Log.d("running", "Clicked on:" + title);
        }

    }

1 个答案:

答案 0 :(得分:0)

在listview的OnItemClick中,您可以创建一个新片段。

很好地解释了片段的创建和处理here

浏览文章以便更好地理解。

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

以上代码直接从开发者网站上挑选。