gpt4 book ai didi

android - 单击 ListView 中的项目时显示 fragment

转载 作者:太空宇宙 更新时间:2023-11-03 12:53:32 25 4
gpt4 key购买 nike

我在 fragment 类中有一个 ListView ,它从 JSON 获取数据。到目前为止,我能够在 ListView 中显示数据。 (这里的数据是新闻项目)。我想在 ListView 中单击特定新闻时显示新闻的详细页面。

例如:Listview 仅包含新闻的标题和图像。单击该 ListView 时,它应该显示该特定新闻 fragment 的详细版本。我该怎么做?

这是具有 Listview 的 fragment 类。

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();
}

}


}

在代码中,您会注意到当单击某个项目时,它会移动到 NewsDetailFragment。这就是我现在要编码的类(class)。

PS:我的 JSON 已经包含所有详细信息,包括标题、图像、描述..

更新::这是我的 NewsDetailFragment 类。它一次显示所有新闻,而不是我点击的那个。

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();
}

}


}

最佳答案

您可以使用以下内容

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();

编辑

FragmentTransaction 用于执行添加或删除 fragment 等操作,即当您替换/更改现有布局时,我们执行此类事务以替换 fragment 。Bundle 是最方便的工具,用于在配置更改时保存数据更改或将数据从一个 Activity 传递到另一个 Activity 或在 fragment 之间传递数据。 Fragments读这个。

对于 link1link2docs . bundle 非常容易理解。

希望编辑有所帮助。

关于android - 单击 ListView 中的项目时显示 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25302698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com