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 <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 <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 android.support.v4.app.Fragment; 3 android.view.LayoutInflater; 4 android.view.View; 5 android.view.ViewGroup; 6 android.widget.ImageView; 7 android.widget.TextView; 8 9 10 /** 11 * A simple {@link Fragment} subclass. 12 */ 13 public class ImgFragment extends Fragment { 14 private ImageView imgview; 15 TextView tv; 16 private int imgres; 17 String text; 18 19 //构造方法,传递内容和图片id参数 20 public ImgFragment( 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(true33 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的事件监听
得到内容 2 String cont=mMap.get("context").toString(); 3 得到图片资源 int img=(int)mMap.get("img" 6 可自接通过此处改变控件上的某个图片显示 8 图片显示控件,main_img=(ImageView)findViewById(R.id.main_img); 9 main_img.setImageResource(img); 10 开始Fragment 12 getSupportFragmentManager().beginTransaction() 13 .addToBackStack("xx1") 14 参数1为主布局id,参数2中构造方法要传入图像资源和展示内容 15 .replace(R.id.main_view,1)">new ImgFragment(img,cont)) 16 .commit(); 17 callbool=true;
Activity的返回和退出:
物理返回键 callbool标志位是为了先销毁Fragment,所以每次打开Fragment是都要设置callbool=true; void onBackPressed() { 4 super.onBackPressed();关闭原有功能 5 if(callbool){ 6 将Fragment退栈 7 getSupportFragmentManager().popBackStack();callbool=; 9 else { 10 关闭程序 11 MainActivity.this.finish(); 12 System.exit(0 } 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); main_ref.setBackgroundResource(R.mipmap.zcy4);listview的背景 3 main_ref.setProgressBackgroundColorSchemeColor(Color.RED);刷新控件的背景 4 5 main_ref.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {刷新监听 7 onRefresh() { 8 清除list中存在的所有数据 9 if(listdatax.size()>0) listdatax.clear(); 已经省略重新获取数据 ............. 12 在获取数据后重新调用适配器设置数据显示 main_list.setAdapter(myadapterx); 在适配器类中设置数据完毕时,关闭动画 15 main_ref.setRefreshing(false);关闭刷新动画 16 17 18 });
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。