简体   繁体   English

单击列表视图中的项目时显示片段

[英]Display a fragment when an item in a listview is clicked

I have a listview in a fragment class which gets data from a JSON. 我在片段类中有一个listview,它从JSON获取数据。 So far i was able to show the data in a listview. 到目前为止,我能够在列表视图中显示数据。 (here data is NEWS items) .I want to display a detailed page of the news when a particular news is clicked in the listview. (这里的数据是新闻项目)。我想在列表视图中点击特定新闻时显示新闻的详细页面。

Ex: Listview contains only the title and image of the news. 例如:列表视图仅包含新闻的标题和图像。 When clicking that listview it should display a detailed version of that particulat news segment. 单击该列表视图时,它应显示该特定新闻片段的详细版本。 How can i do this? 我怎样才能做到这一点?

This is the fragment class that has the Listview. 这是具有Listview的片段类。

package com.fortuna.cinemalk;

import java.util.ArrayList;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.content.Intent;
import android.widget.AdapterView;



import com.fortuna.cinemalk.adapter.LazyAdapter;
import com.fortuna.cinemalk.model.BaseElement;
import com.fortuna.cinemalk.service.CommonVariable;
import com.fortuna.cinemalk.service.JSONServices;
import com.fortuna.cinemalk.util.Element;

public class NewsFramgment extends Fragment {

    private GridView gridView;
    private ListView listView;

    private ArrayList<BaseElement> News;
    private LazyAdapter adapter;
    private Activity activity;
    private CommonVariable commonVariable;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.news_fragment, container,
                false);

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();

        //gridView = (GridView) view.findViewById(R.id.gridView2);
        listView = (ListView) view.findViewById(R.id.list);


        listView.setOnItemClickListener(new OnItemClickListener() {
               public void onItemClick(AdapterView<?> parent, View v,
                 int position, long id) {


              android.support.v4.app.Fragment detail = new NewsDetailFragment();
              android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction().add(R.id.content_frame, detail).addToBackStack("back").commit();
               }
              });



            new BackGround().execute();

        return view;
    }


public class BackGround extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            News = JSONServices.getNewsDescription();
            return null;
        } 



        @Override
        /* check again */
        protected void onPostExecute(Void result) {

            commonVariable.setNewsDescription(News);

            adapter = new LazyAdapter(News, activity,Element.NEWS_LIST.getType());

            listView.setAdapter(adapter);

            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

    }


}

In the code you'll notice that when a item is clicked it moves to the NewsDetailFragment. 在代码中,您会注意到单击某个项目时,它将移至NewsDetailFragment。 And that's the class i want to code now. 这就是我想要编写的类。

PS: my JSON already contains all the details including title,images,description.. PS:我的JSON已经包含了所有细节,包括标题,图片,描述......

UPDATE:: This is my NewsDetailFragment class. UPDATE ::这是我的NewsDetailFragment类。 It shows all the news at once not the one i clicked. 它显示所有新闻,而不是我点击的那个。

package com.fortuna.cinemalk;

import java.util.ArrayList;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.content.Intent;
import android.widget.AdapterView;



import com.fortuna.cinemalk.adapter.LazyAdapter;
import com.fortuna.cinemalk.model.BaseElement;
import com.fortuna.cinemalk.service.CommonVariable;
import com.fortuna.cinemalk.service.JSONServices;
import com.fortuna.cinemalk.util.Element;

public class NewsDetailFragment extends Fragment {

    private GridView gridView;
    private View view1;

    private ArrayList<BaseElement> newsdetail;
    private LazyAdapter adapter;
    private Activity activity;
    private CommonVariable commonVariable;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.newsdetail_fragment, container,
                false);

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();

        view1 = (View) view.findViewById(R.id.list);


        /*gridView.setOnItemClickListener(new OnItemClickListener() {
               public void onItemClick(AdapterView<?> parent, View v,
                 int position, long id) {


              android.support.v4.app.Fragment detail = new TheaterDetailFragment();
              android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction().add(R.id.content_frame, detail).addToBackStack("back").commit();
               }
              }); */



            new BackGround().execute();

        return view;
    }


public class BackGround extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            newsdetail = JSONServices.getNewsDescription();
            return null;
        } 



        @Override
        /* check again */
        protected void onPostExecute(Void result) {

            commonVariable.setTheater(newsdetail);

            adapter = new LazyAdapter(newsdetail, activity,Element.NEWS_DETAIL.getType());

            ((AdapterView<ListAdapter>) view1).setAdapter(adapter);

            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

    }


}

You can use following 你可以使用以下

FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                .beginTransaction();
Fragment profileFragment = new MovieDetailFragment();//the fragment you want to show
profileFragment.setArguments(bundle);
fragmentTransaction
    .replace(R.id.content_frame, profileFragment);//R.id.content_frame is the layout you want to replace
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

EDIT 编辑

FragmentTransaction are use to perform actions like adding or removing the fragment ie when ur replacing/changing the existing layout we perform such transaction to replace the fragments. FragmentTransaction用于执行添加或删除片段等操作,即当您更换/更改现有布局时,我们执行此类事务以替换片段。 Bundles are the most handly tool that is required to save the changes in data when the configuration changes or to pass the data from one activity to other or to pass the data between the fragments. 捆绑包是配置更改时保存数据更改或将数据从一个活动传递到另一个活动或在片段之间传递数据所需的最便捷的工具。 Fragments read this. 片段读了这个。

For link1 and link2 and the docs . 对于link1link2以及docs Bundles are very easy to understand. 捆绑包很容易理解。

Hope the edit helps. 希望编辑有所帮助。

i was doing this way... 我是这样做的......

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.frag_for_accsummary,container, false);
       final FragmentTransaction ft=    getFragmentManager().beginTransaction();


      //geting resources
    Resources res=getActivity().getResources();
    title=res.getStringArray(R.array.title);
        // TODO Auto-generated method stub

    ListView l=(ListView)v.findViewById(R.id.list);
    MyAadapter m=new MyAadapter(getActivity(),R.layout.singlerow, title, icon);//custom adapter
    //title--> array of text to be shown on list    icon-->  array of images to be shown on list     R.layout.singlerow--> tells how single row should appear in list
    l.setAdapter(m);

    l.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            // TODO Auto-generated method stub
            Log.i("in seton","setonclick");

            switch(position){

            case 0: 
                AccSummary frag0=new AccSummary();

            bun.putString("uname",uname);
            bun.putString("sessid",sessid);
            bun.putString("custid",custid1);
            frag0.setArguments(bun);
            ft.replace(R.id.lin2,frag0);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.addToBackStack(null);
            ft.commit();
            Log.i("FragFor", uname);

                break;

    //-------------------------------------------------------------------       

            case 1:
                      FromAcc acc=new FromAcc();
                      bun.putString("Name",uname); 
                      bun.putString("sessid",sessid);// key - value pair
                      bun.putString("custid",custid1);
                      acc.setArguments(bun);
                      ft.replace(R.id.lin2, acc);
                      ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                        ft.addToBackStack(null);
                        ft.commit();


            break;
            case 2:break;
            case 3:break;
            case 4:break;
            case 5:ChangepassFrag cf=new ChangepassFrag();
                  bun.putString("uname",uname);
                  bun.putString("sessid",sessid);
                  bun.putString("custid",custid);
                  bun.putString("password",pass);
                 cf.setArguments(bun);
                    ft.replace(R.id.lin2,cf);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    ft.addToBackStack(null);
                    ft.commit();
                    break;

            case 6:   break;
            case 7:break;
            case 8:break;
            case 9:
                   Fragment1 f=new Fragment1();
                   bun.putString("uname",uname);
                    bun.putString("sessid",sessid);
                //  bun.putString("custid",custid1);
                    f.setArguments(bun);
                        ft.replace(R.id.lin2,f);
                        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                        ft.addToBackStack(null);
                        ft.commit();
                break;
            case 10:break;
            }




            }





    });





    return v;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击ListView项时打开一个新片段 - Opening a new fragment when a ListView item is clicked 单击菜单项时无法显示片段 - Not able to display fragment when menu item is clicked 单击列表视图中的项目时显示图像 - display image when clicked on the item in listview 使用arraylist在列表视图的项目上单击时如何显示列表视图? - how to display a listview when clicked on the item of a listview with the use of arraylist? Android如何在单击ListView项目时隐藏和显示ListView中的项目 - Android How to hide and display items in a ListView when a ListView item is clicked Listview 项目不显示在 Fragment Android 中 - Listview item not Display in the Fragment Android 使用onitemselected侦听器单击微调器的项目时如何显示列表视图? - how to display a listview when clicked on the item of spinner using onitemselected listener? Android-充气片段取决于单击的listview项 - Android - Inflate fragment depending on listview item clicked 片段中的ListView Clickable和on item clicked listener - ListView in a fragment Clickable and on item clicked listener 当从列表视图片段中单击特定项目时,如何使用webview打开片段中的链接 - How to open link in a fragment with webview when a specific item is clicked from listview fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM