![](/img/trans.png)
[英]How to detect tablet layout and open two layout fragments on the screen (Like master detail fragments)
[英]How to display two fragments vertically (master and detail) in one activity
我試圖通過從ListView中選擇一個元素來顯示信息,我正在從一個Fragment中執行該操作,並且試圖調用另一個Fragment來顯示,並且從SQL數據庫中獲取信息,並且我正在嘗試將其發送到另一個Fragment。 這是我的代碼:這是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>
這是XML格式TestimoniosDetallado:
<?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>
而不是替換布局,它顯示在列表視圖下方: 代碼的結果
將背景(顏色或可繪制)設置為TestimoniosDetallado布局。 由於未設置TestimoniosDetallado布局的背景,因此它是透明的,因此在后面顯示了您的TestimoniosFragment布局。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.