[Android]Fragment应用/ListFragment/DialogFragment
来源:互联网 发布:生成淘宝无线链接地址 编辑:程序博客网 时间:2024/05/21 08:57
》》ListFragment以List的形式展现,如果只是简单地展现一个List列表,这是一个不错的选择
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.girls.tiffany270.list_dialog_fragment.MainActivity"> <fragment android:id="@+id/fragment_list" android:name="com.girls.tiffany270.list_dialog_fragment.List_Fragmet" android:layout_width="match_parent" android:layout_height="match_parent"> </fragment></RelativeLayout>
这里的id是固定值
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!--这里的Listview的id是系统id,要规范--> <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView></RelativeLayout>
package com.girls.tiffany270.list_dialog_fragment;import android.app.ListFragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ListView;import java.util.ArrayList;import java.util.List;/** * Created by Tiffany270 on 2017/11/10. */public class List_Fragmet extends ListFragment { private List<String> list;//准备数据源 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.list_fragment, null); list = new ArrayList<>();//实例化数据源 //模拟数据 for (int i = 0; i < 20; i++) { list.add("itme" + i); } ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(), android.R.layout.simple_list_item_1,list); //这里默认已经有Listview了 setListAdapter(adapter); return view; } //这里可以设置点击事件 @Override public void onListItemClick(ListView l, View v, int position, long id) { //list.get(position); }}
效果:
传统的dialog在转动屏幕会有点问题,官方是不建议直接使用,所以衍生除了DialogFragment
package com.girls.tiffany270.list_dialog_fragment;import android.app.DialogFragment;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view){ DialogFragment fragment = new Dialog_Fragment(); fragment.show(getFragmentManager(),"dialog"); }}
package com.girls.tiffany270.list_dialog_fragment;import android.app.AlertDialog;import android.app.Dialog;import android.app.DialogFragment;import android.content.DialogInterface;import android.os.Bundle;/** * Created by Tiffany270 on 2017/11/10. */public class Dialog_Fragment extends DialogFragment { // //使用xml文件来定义对话框 // @Nullable // @Override // public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { // return super.onCreateView(inflater, container, savedInstanceState); // } //用alerdailog或dailog @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("Dialog"); builder.setMessage("你确定要退出吗?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //设置监听事件 } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //设置事件 } }); return builder.create(); }}实现效果
阅读全文
0 0
- [Android]Fragment应用/ListFragment/DialogFragment
- Fragment的两个子类ListFragment和DialogFragment的应用Demo
- android----fragment学习中的listFragment
- DialogFragment和ListFragment使用
- android DialogFragment 回调到 Fragment
- 【Android Fragment】DialogFragment怎么玩?
- Android系列之Fragment----ListFragment的使用
- Android Fragment的使用 八 ListFragment
- android DialogFragment简单应用
- android DialogFragment简单应用
- Fragment - DialogFragment
- Fragment+DialogFragment
- 一个Android练手工程(ViewPager、Fragment、ListFragment、sqlite)
- Android之Fragment-ListFragment的实践(三)
- Android Fragmnet-Fragment数据交换以及ListFragment的使用
- Android ListFragment
- android Listfragment
- Android ListFragment
- LeetCode题解 第八周
- Linux网络编程(一)
- oracle----表空间
- geopoint pointer 类型的存或取
- Minus intersect Union UnionAll 使用方法
- [Android]Fragment应用/ListFragment/DialogFragment
- Nio-FileChannel与粘包问题
- 初学者--Android Retrofit+RxJava+OkHttp使用post请求
- 比特币源码解析(20)
- java基础-数组及排序
- Elasticsearch-bool组合查询
- 解决react-native软键盘弹出挡住输入框的问题
- learn opencv-Ubuntu安装opencv3
- 六边形的代码画法