我有这个listview是新闻列表。在列表视图中单击某个新闻项时,我想显示该新闻项的详细版本。
到目前为止,我已经能够创建列表视图并在列表视图中显示新闻项。(注意:新闻项来自JSON)
在列表视图中单击新闻项时,我能够显示一个视图。但问题是单击列表视图项时,将显示新闻项详细版本的视图,但显示所有新闻的详细版本。
我只想显示已选择的新闻项目的详细版本。我怎样才能做到这一点?
经过研究,我认为我应该为此使用捆绑。但我不知道如何做到这一点。
我会在这里张贴我的课程
NewsFragment.java
public class NewsFramgment extends Fragment {
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();
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();
}
}
}
NewsDetailFramgment.java
public class NewsDetailFragment extends Fragment {
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);
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();
}
}
}
看来您正在呼叫newsdetail = JSONServices.getNewsDescription();
,然后将该结果分配给adapter
in NewsDetailFragment
。返回的是JSONServices.getNewsDescription();
什么?我想这就是所有NewsDetail文章。
@Raghunandan是正确的,该示例显示了如何处理活动中的两个片段,并根据“新闻”列表中选择的项目显示了“详细信息”片段。
您可以在父活动中实现调用,如“实现接口”下的示例所示。
YourParentActivity.java
public static class YourParentActivity extends Activity {
implements NewsFragment.OnHeadlineSelectedListener{
...
}
public void onArticleSelected(int position) {
NewsDetailFramgment articleFrag = (NewsDetailFramgment)
getSupportFragmentManager().findFragmentById(R.id.news_detail_framgment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the NewsDetailFramgment to update its content
articleFrag.updateArticleView(position);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
NewsDetailFramgment newFragment = new NewsDetailFramgment ();
Bundle args = new Bundle();
args.putInt(NewsDetailFramgment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
}
注意onArticleSelected()
接受int position
吗?这是在“新闻头条”适配器中被单击的位置。您可以使用它来从“详细信息”结果中获取相应的“新闻详细信息”项。
这纯粹是基于示例的概述,但应向您展示您在父级活动中需要执行的操作。
阅读示例并将相关部分添加到您的片段中(请参阅“定义接口”以了解对您所做的更改NewsFragment.java
)。
编辑
您需要在NewsFragment
类中定义一个接口,如“定义接口”下的文档所示。
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
当您按下新闻标题和所选标题的位置时,这将为您的父类提供一个回调,以进行处理。在示例中,它调用onArticleSelected(int position);
您的父活动,然后填充NewsDetailFragment
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句