Android fragments 在listview项目中使用progressBar

Android fragments 在listview项目中使用progressBar,android-fragments,android-listview,progress-bar,Android Fragments,Android Listview,Progress Bar,我有这个listview,它是在一个片段类中编码的。选择listview项目时,它会转到另一个片段并显示详细信息。但是加载数据需要几秒钟,因为我显示的数据使用的是AsyncTask,所以从JSON和display获取数据需要一些时间。因此,当在listview中单击某个项目时,我需要使用progressBar,当数据准备好显示时,它应该关闭。我该怎么做?我参考了一些教程,并尝试了其中的许多。但我没能完成我想要的。希望你们中的一些人能帮助我 新闻片段类 private ListView listV

我有这个listview,它是在一个片段类中编码的。选择listview项目时,它会转到另一个片段并显示详细信息。但是加载数据需要几秒钟,因为我显示的数据使用的是AsyncTask,所以从JSON和display获取数据需要一些时间。因此,当在listview中单击某个项目时,我需要使用progressBar,当数据准备好显示时,它应该关闭。我该怎么做?我参考了一些教程,并尝试了其中的许多。但我没能完成我想要的。希望你们中的一些人能帮助我

新闻片段类

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) {

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

    }

}
私有ListView ListView;
私人ArrayList新闻;
专用懒散适配器;
私人活动;
私有公共变量公共变量;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
最终视图=充气机充气(R.layout.news_碎片、容器、,
假);
activity=this.getActivity();
commonVariable=(commonVariable)activity.getApplication();
listView=(listView)view.findViewById(R.id.list);
setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、,
int位置,长id){
android.support.v4.app.Fragment detail=新的NewsDetailFragment();
android.support.v4.app.FragmentManager FragmentManager=getFragmentManager();
fragmentManager.beginTransaction().add(R.id.content_frame,detail).addToBackStack(“back”).commit();
}
});
新建背景().execute();
返回视图;
}
公共类后台任务{
@凌驾
受保护的Void doInBackground(Void…参数){
News=JSONServices.getNewsDescription();
返回null;
} 
@凌驾
/*再次检查*/
受保护的void onPostExecute(void结果){
commonVariable.setNewsDescription(新闻);
adapter=newlazyadapter(新闻、活动、元素.News_LIST.getType());
setAdapter(适配器);
super.onPostExecute(结果);
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
}
}
}
新闻细节片段

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;

        } 

@SuppressWarnings("unchecked")
@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();
        }

    }
}
公共类NewsDetailFragment扩展了片段{
私有视图1;
私有ArrayList newsdetail;
专用懒散适配器;
私人活动;
私有公共变量公共变量;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.newsdetail_碎片,容器,
假);
activity=this.getActivity();
commonVariable=(commonVariable)activity.getApplication();
view1=(View)View.findViewById(R.id.list);
新建背景().execute();
返回视图;
}
公共类后台任务{
@凌驾
受保护的Void doInBackground(Void…参数){
newsdetail=JSONServices.getNewsDescription();
返回null;
} 
@抑制警告(“未选中”)
@凌驾
/*再次检查*/
受保护的void onPostExecute(void结果){
commonVariable.setTheater(newsdetail);
adapter=newLazyAdapter(newsdetail、activity、Element.NEWS\u DETAIL.getType());
((AdapterView)视图1.setAdapter(适配器);
super.onPostExecute(结果);
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
}
}
}
新闻列表布局

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/grey"
     >


    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/listview" >

       <ImageView
        android:id="@+id/thumb_image"
        android:layout_width="150dp"
        android:layout_height="120dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp"


        /> 


        <TextView
        android:id="@+id/news_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:layout_toRightOf="@+id/thumb_image"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="7dp"
        android:textStyle="bold"
        android:textSize="20dp" />

    <!--  <TextView
        android:id="@+id/news_description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textStyle="bold"
        android:layout_below="@+id/news_title"
        android:textSize="15dp" />   -->

    </LinearLayout>

</RelativeLayout>

新闻片段布局

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


  <!--  <GridView
        android:id="@+id/gridView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:numColumns="2"
        android:stretchMode="columnWidth" 
        android:layout_margin="5dp"
        android:columnWidth="100dp"/>   --> 

        <ListView
              android:id="@+id/list"
              android:layout_height="match_parent"
              android:layout_width="match_parent"
              android:dividerHeight="10dp"
              android:layout_margin="6dp">
         </ListView>

</LinearLayout>

ProgressBar
添加到新闻片段版面:

<ProgressBar 
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:indeterminate="true"
    android:visibility="gone" />


然后在AsyncTask的
onPostExecute
中,在
OnProgressBar.setVisibility(View.VISIBLE)
方法中将其可见性更改为true以显示它,并将其可见性更改为false以隐藏它
progressBar.setVisibility(View.GONE)
。我这样修改了我的第二个片段。当listview中的一个项目被点击时,它会进入我的第二个类。所以我在第二个类中添加了一个progressBar,它成功了

public class NewsDetailFragment extends Fragment {

private View view1;

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


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

        dialog = new ProgressDialog(NewsDetailFragment.this.getActivity());
        dialog.setMessage("Loading News");
        dialog.setCancelable(true);

        new BackGround().execute();

        return view;
    }



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

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

            newsdetail = JSONServices.getNewsDescription();

            return null;

        } 

@SuppressWarnings("unchecked")
@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);

            dialog.dismiss();

            super.onPostExecute(result);
        }

@Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub

    dialog.show();
            super.onPreExecute();
        }

    }

}
公共类NewsDetailFragment扩展了片段{
私有视图1;
私有ArrayList newsdetail;
专用懒散适配器;
私人活动;
私有公共变量公共变量;
私人对话;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.newsdetail_碎片,容器,
假);
activity=this.getActivity();
commonVariable=(commonVariable)activity.getApplication();
view1=(View)View.findViewById(R.id.list);
dialog=newprogressdialog(NewsDetailFragment.this.getActivity());
setMessage(“加载新闻”);
对话框。可设置可取消(true);
新建背景().execute();
返回视图;
}
公共类后台任务{
@凌驾
受保护的Void doInBackground(Void…参数){
newsdetail=JSONServices.getNewsDescription();
返回null;
} 
@抑制警告(“未选中”)
@凌驾
/*再次检查*/
受保护的void onPostExecute(void结果){
commonVariable.setTheater(newsdetail);
adapter=newLazyAdapter(newsdetail、activity、Element.NEWS\u DETAIL.getType());
((AdapterView)视图1).设置适配器