如何在一个活动中垂直显示两个片段(主和细节)

时间:2015-11-17 05:46:14

标签: android android-layout listview android-fragments

我正在尝试通过从ListView中选择一个元素来显示信息,我正在从片段中执行该操作,并且我正在尝试调用另一个片段来显示。我正在从SQL中提取信息数据库,我正在尝试将其发送到另一个片段。 这是我的代码: 这是TestimoniosFragment:

public class TestimoniosFragment extends Fragment {


boolean isDualPane;

ListView listView;
TextView idTestimonio;
TextView nombre;
TextView frase;

ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

//URL to get JSON Array
private static String url = "http://ucwm.co.nf/Testimonio_app.php";

//JSON Node Names
private static final String TAG_TESTIMONIO = "testimonios";
private static final String TAG_ID = "IdTestimonio";
private static final String TAG_NOMBRE = "NombreSobreviviente";
private static final String TAG_FRASE = "Frase";

JSONArray testimonios = null;

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

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

    //((MainActivity) getActivity()).getSupportActionBar().setTitle("Eventos");

    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    oslist = new ArrayList<HashMap<String, String>>();

    new JSONParse().execute();

}

private class JSONParse extends AsyncTask<String, String, JSONObject> {

    private ProgressDialog pDialog;
    JSONParser jParser = new JSONParser();
    private JSONObject json;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        idTestimonio = (TextView) getView().findViewById(R.id.txt1);
        nombre = (TextView) getView().findViewById(R.id.txt2);
        frase = (TextView) getView().findViewById(R.id.txt3);

        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Cargando Datos...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... args) {

        // Getting JSON from URL
        json = jParser.getJSONFromUrl(url);

        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {

        pDialog.dismiss();
        try {
            // Getting JSON Array from URL
            testimonios = json.getJSONArray(TAG_TESTIMONIO);

            for (int i = 0; i < testimonios.length(); i++) {

                JSONObject c = testimonios.getJSONObject(i);

                // Storing  JSON item in a Variable
                final String idTestimonio = c.getString(TAG_ID);
                String nombre = c.getString(TAG_NOMBRE);
                String frase = c.getString(TAG_FRASE);

                // Adding value HashMap key => value
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_ID, idTestimonio);
                map.put(TAG_NOMBRE, nombre);
                map.put(TAG_FRASE, frase);

                oslist.add(map);

                listView = (ListView) getView().findViewById(R.id.listView3);

                ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                        R.layout.list_item,
                        new String[]{TAG_ID,TAG_NOMBRE, TAG_FRASE}, new int[]{
                        R.id.txt1, R.id.txt2,R.id.txt3});
                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                        FragmentManager fm = getFragmentManager();
                        FragmentTransaction ft = fm.beginTransaction();
                        ft.replace(R.id.containerT, new TestimoniosDetallado());
                        ft.commit();
                    }
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
}

这是TestimoniosDetallado:

public class TestimoniosDetallado extends Fragment {

//Bundle bundle = this.getArguments();
//String strtext = bundle.getString("mensaje", "idTestimonio");

String strtext;

//private long id;
//private String nombre;
TextView nomb_sobreviviente_text;
TextView frase_text;
TextView descripcion_text;

ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

//URL to get JSON Array
private static String url = "http://ucwm.co.nf/Testimonio_app.php";

//JSON Node Names
private static final String TAG_TESTIMONIO = "testimonios";
private static final String TAG_ID = "IdTestimonio";
private static final String TAG_NOMBRE = "NombreSobreviviente";
private static final String TAG_FRASE = "Frase";
private static final String TAG_DESCRIPCION = "Descripcion";

JSONArray eventos = null;

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

    View view = inflater.inflate(R.layout.testimonio_detallado, container, false);
    //strtext= getArguments().getString("mensaje");
    //((MainActivity) getActivity()).getSupportActionBar().setTitle("Eventos");

    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    oslist = new ArrayList<HashMap<String, String>>();

    new JSONParse().execute();

}

private class JSONParse extends AsyncTask<String, String, JSONObject> {

    private ProgressDialog pDialog;
    JSONParser jParser = new JSONParser();
    private JSONObject json;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        nomb_sobreviviente_text = (TextView) getView().findViewById(R.id.nomb_sobreviviente);
        frase_text = (TextView) getView().findViewById(R.id.frase);
        descripcion_text  = (TextView) getView().findViewById(R.id.descripcion);

        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Cargando Datos...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... args) {

        // Getting JSON from URL
        json = jParser.getJSONFromUrl(url);

        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {

        pDialog.dismiss();
        try {
            // Getting JSON Array from URL
            eventos = json.getJSONArray(TAG_TESTIMONIO);

            for (int i = 0; i < eventos.length(); i++) {
                JSONObject c = eventos.getJSONObject(i);

                // Storing  JSON item in a Variable
                String id = c.getString(TAG_ID);
                String nombre = c.getString(TAG_NOMBRE);
                String frase = c.getString(TAG_FRASE);
                String descripcion = c.getString(TAG_DESCRIPCION);
                /*

                if(strtext.equalsIgnoreCase(id)){

                    nomb_sobreviviente_text.setText(nombre);
                    frase_text.setText(frase);
                    descripcion_text.setText(descripcion);
                    // Adding value HashMap key => value
                    /*
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_NOMBRE, nombre);
                    map.put(TAG_FRASE, frase);
                    map.put(TAG_DESCRIPCION, descripcion);

                    //oslist.add(map);

                    //listView = (ListView) getView().findViewById(R.id.listView3);

                    //ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                    //        R.layout.list_item,
                    //        new String[]{TAG_NOMBRE, TAG_FRASE,TAG_DESCRIPCION}, new int[]{
                    //        R.id.txt1, R.id.txt2});
                    //listView.setAdapter(adapter);
                }


                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Toast.makeText(getActivity(), "You Clicked at " + oslist.get(+position).get("txt1"), Toast.LENGTH_SHORT).show();
                    }
                });
                */
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
}

以下是来自TestimoniosFragment的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/containerT">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:paddingTop="48dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView3"
            android:layout_weight="1" />
        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

以下是TestimoniosDetallado的XML表单:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="nombre Sobreviviente"
        android:id="@+id/nomb_sobreviviente"
        android:layout_marginTop="139dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Frase"
        android:id="@+id/frase"
        android:layout_below="@+id/nomb_sobreviviente"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="151dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Descripcion"
        android:id="@+id/descripcion"
        android:layout_below="@+id/frase"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="111dp" />
    </RelativeLayout>

而不是替换布局,它显示在列表视图下方: Result from the code

1 个答案:

答案 0 :(得分:0)

将背景(颜色或可绘制)设置为TestimoniosDetallado布局。它显示了你的TestimoniosFragment布局,因为你的TestimoniosDetallado布局的背景没有设置,所以它是透明的。