1. 程式人生 > >ListView行詳細內容展示頁編寫和下拉重新整理實現

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