安卓ListView行详细内容展示页编写和下拉刷新实现

ListView行详细内容展示页:

使用轻量级的Fragment实现Listview行内容简单的详细信息展示:

值得注意的是:

1、 主布局(打开它的Activity)必须是FrameLayout布局(帧布局,上下叠加)

2、如果主布局的按钮不能被覆盖,则可在按钮属性加入:android:stateListAnimator="@null"

3、防止穿透点击可在Fragment类中找到视图后添加:view对象.setClickable(true);

4、如果使用的是import android.support.v4.app.Fragment;包时则操作使用getSupportFragmentManager,否则如果使用的是import android.app.Fragment;包时则操作使用getFragmentManager

 

Fragment类的布局编写:

 1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5 
 6     tools:context="com.example.httptest.ImgFragment">
 7 
 8 <ImageView
 9     android:layout_width="match_parent"
10     android:background="@color/colorBlack"
11     android:alpha="0.9"
12     android:layout_height="match_parent" />
13 
14     <ImageView
15         android:layout_width="match_parent"
16         android:src="@mipmap/zcy4"
17         android:layout_gravity="center"
18         android:id="@+id/ff_img"
19         android:layout_height="match_parent" />
20     <TextView
21         android:layout_width="wrap_content"
22         android:text="图片展示页:"
23 
24         android:textSize="18dp"
25         android:layout_height="wrap_content" />
26 
27     <TextView
28         android:layout_width="wrap_content"
29         android:layout_marginTop="20dp"
30         android:text="默认内容"
31         android:id="@+id/ff_title"
32         android:textColor="@color/colorRed"
33         android:layout_gravity="center|top"
34         android:layout_height="wrap_content" />
35 </FrameLayout>

Fragment类逻辑编写:

 1 import android.os.Bundle;
 2 import android.support.v4.app.Fragment;
 3 import android.view.LayoutInflater;
 4 import android.view.View;
 5 import android.view.ViewGroup;
 6 import android.widget.ImageView;
 7 import android.widget.TextView;
 8 
 9 
10 /**
11  * A simple {@link Fragment} subclass.
12  */
13 public class ImgFragment extends Fragment {
14     private ImageView imgview;
15     private TextView tv;
16     private int imgres;
17     private String text;
18 
19     //构造方法,传递内容和图片id参数
20     public ImgFragment(int imgres,String text) {
21         this.imgres=imgres;
22         this.text=text;
23     }
24 
25 
26     @Override
27     public View onCreateView(LayoutInflater inflater, ViewGroup container,
28                              Bundle savedInstanceState) {
29         View ffview=inflater.inflate(R.layout.fragment_img, container, false);
30 
31         //设置不能穿透点击
32         ffview.setClickable(true);
33 
34         imgview=(ImageView)ffview.findViewById(R.id.ff_img);
35         tv=(TextView)ffview.findViewById(R.id.ff_title);
36 
37         //显示
38         tv.setText(text);
39         imgview.setImageResource(imgres);
40 
41         return ffview;
42     }
43 
44 }

Activity的ListView监听事件里的部分代码:

基于上一篇的SimpleAdapter的事件监听

 1 //得到内容
 2 String cont=mMap.get("context").toString();
 3 
 4 //得到图片资源
 5 int img=(int)mMap.get("img");
 6 
 7 //可自接通过此处改变控件上的某个图片显示
 8 //图片显示控件,main_img=(ImageView)findViewById(R.id.main_img);
 9 main_img.setImageResource(img);
10 
11 //开始Fragment
12 getSupportFragmentManager().beginTransaction()
13         .addToBackStack("xx1")
14         //参数1为主布局id,参数2中构造方法要传入图像资源和展示内容
15         .replace(R.id.main_view,new ImgFragment(img,cont))
16         .commit();
17 callbool=true;

Activity的返回和退出:

 1 //物理返回键
 2 //callbool标志位是为了先销毁Fragment,所以每次打开Fragment是都要设置callbool=true;
 3 public void onBackPressed() {
 4     // super.onBackPressed();关闭原有功能
 5  if(callbool){
 6      //将Fragment退栈
 7         getSupportFragmentManager().popBackStack();callbool=false;
 8     }
 9     else {
10         //关闭程序
11         //MainActivity.this.finish();
12          System.exit(0);
13  }
14     }

 

Listview下拉刷新实现:

基于上一篇的BaseAdapt视图及数据实现

界面内容改变,套入SwipeRefreshLayout:

 1 <android.support.v4.widget.SwipeRefreshLayout
 2     android:layout_width="match_parent"
 3     android:id="@+id/main_ref"
 4     android:layout_marginTop="200dp"
 5     android:layout_height="match_parent">
 6     <ListView
 7         android:layout_width="match_parent"
 8         android:id="@+id/main_list"
 9         android:layout_height="match_parent">
10     </ListView>
11 </android.support.v4.widget.SwipeRefreshLayout>

必须设置此处:

设置为一个全局对象,不能每次数据获取都去建立新对象,否则需要每次初始化适配器类。

private  List<BaseData> listdatax=new ArrayList<>();

刷新控件监听:

 1 main_ref=(SwipeRefreshLayout)findViewById(R.id.main_ref);
 2 //main_ref.setBackgroundResource(R.mipmap.zcy4);//listview的背景
 3 main_ref.setProgressBackgroundColorSchemeColor(Color.RED);//刷新控件的背景
 4 
 5 main_ref.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {//刷新监听
 6     @Override
 7     public void onRefresh() {
 8         //清除list中存在的所有数据
 9         if(listdatax.size()>0) listdatax.clear();
10         //已经省略重新获取数据
11         //.............
12         //在获取数据后重新调用适配器设置数据显示
13         main_list.setAdapter(myadapterx);
14         //在适配器类中设置数据完毕时,关闭动画
15         main_ref.setRefreshing(false);//关闭刷新动画
16 
17     }
18 });

 


 

posted @ 2019-01-02 16:41  东小东  阅读(527)  评论(0编辑  收藏  举报