博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 用两个fragment实现列表选择
阅读量:6077 次
发布时间:2019-06-20

本文共 9983 字,大约阅读时间需要 33 分钟。

hot3.png

   看了魏小翔做的用两个fragment实现滑动列表选择的效果,觉得真的很不错,自己就仿照做了一个,觉得fragment真的很好用,在activity中使用,减少了一个activity使用的复杂程度。

效果图:

 

 


  • FindDoctorActivity.java

主activity不需要做什么,只需要把放fragment的布局加载进来进行了

public class FindDoctorActivity  extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_find_doctor);        TranslucentBarsUtils.setColor(this, Color.parseColor("#35b4c2"));        ButterKnife.bind(this);        initData();    }    private void initData() {    }
  • activity_find_doctor.xml
  • BranchListFragment.java   这是左侧选择的fragment,需要实现setOnItemClickListener将选择的id信息传递给右侧,其实fragment里面的原理就类似于activity,也有自己的视图和数据处理,对没错,他就是需要绑定自己的布局文件。

    这里的fragment_branch_list.xml布局文件就是一个Listview列表,需要用适配器实现里面的效果,这里就不贴代码了。

 

package com.mialab.healthbutler.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.ListView;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import com.mialab.healthbutler.R;import com.mialab.healthbutler.adapter.BranchAdapter;import com.mialab.healthbutler.domain.Branch;import com.mialab.healthbutler.domain.ResponseResult;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.List;import butterknife.BindView;import butterknife.ButterKnife;/** * Created by hp on 2016/6/10. */public class BranchListFragment extends Fragment {    private static final String branches = "{\n" +            "  \"error\": false,\n" +            "  \"results\": [\n" +            "    {\n" +            "      \"id\": \"1\",\n" +            "      \"branch_name\":\"内科\"\n" +            "    },\n" +            "    {\n" +            "      \"id\": \"2\",\n" +            "      \"branch_name\":\"外科\"\n" +            "    },\n" +            "    {\n" +            "      \"id\": \"3\",\n" +            "      \"branch_name\":\"妇产科学\"\n" +            "    },\n" +            "    {\n" +            "      \"id\": \"4\",\n" +            "      \"branch_name\":\"生殖中心\"\n" +            "    },\n" +            "    {\n" +            "      \"id\": \"5\",\n" +            "      \"branch_name\":\"骨外科\"\n" +            "    },\n" +            "    {\n" +            "      \"id\": \"6\",\n" +            "      \"branch_name\":\"眼科学\"\n" +            "    },\n" +            "    {\n" +            "      \"id\": \"7\",\n" +            "      \"branch_name\":\"五官科\"\n" +            "    },\n" +            "    {\n" +            "      \"id\": \"8\",\n" +            "      \"branch_name\":\"肿瘤科\"\n" +            "    },\n" +            "    {\n" +            "      \"id\": \"9\",\n" +            "      \"branch_name\":\"口腔科学\"\n" +            "    },\n" +            "    {\n" +            "      \"id\": \"10\",\n" +            "      \"branch_name\":\"皮肤性病科\"\n" +            "    },\n" +            "    {\n" +            "      \"id\": \"11\",\n" +            "      \"branch_name\":\"男科\"\n" +            "    },\n" +            "    {\n" +            "      \"id\": \"12\",\n" +            "      \"branch_name\":\"皮肤美容\"\n" +            "    }\n" +            "  ]\n" +            "}";    @BindView(R.id.lv_branch)    ListView lv_branch;    List
list_branchs = new ArrayList
(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_branch_list, container, false); ButterKnife.bind(this, view); initDate(); return view; } private void initDate() { Gson gson = new Gson(); Type userType = new TypeToken
>>() { }.getType(); ResponseResult
> result = gson.fromJson(branches, userType); list_branchs = result.getResults(); final BranchAdapter branchAdapter = new BranchAdapter(getActivity(), list_branchs); lv_branch.setAdapter(branchAdapter); lv_branch.setSelection(0); branchAdapter.setSelectedItem(0); lv_branch.setOnItemClickListener(new AdapterView.OnItemClickListener() { private IllnessListFragment illnessListFragment; /** * 将变化传递给疾病fragment * @param parent * @param view * @param position * @param id */ @Override public void onItemClick(AdapterView
parent, View view, int position, long id) { branchAdapter.setSelectedItem(position); illnessListFragment = (IllnessListFragment) getActivity().getFragmentManager().findFragmentById(R.id.fg_illness); illnessListFragment.notifyDataChange(list_branchs.get(position).getId()); } }); }}

 

  • IllnessListFragment.java

这个fragment需要接受上一个fragment传递过来的id值进行选择性的显示,这个逻辑就不识闲了,贴了死的json代码

package com.mialab.healthbutler.fragment;import android.app.Fragment;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.ListView;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import com.mialab.healthbutler.R;import com.mialab.healthbutler.activity.DoctorListActivity;import com.mialab.healthbutler.activity.HosDetailActivity;import com.mialab.healthbutler.adapter.HospitalAdapter;import com.mialab.healthbutler.adapter.IllnessAdapter;import com.mialab.healthbutler.domain.Hospital;import com.mialab.healthbutler.domain.Illness;import com.mialab.healthbutler.domain.ResponseResult;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.List;import butterknife.BindView;import butterknife.ButterKnife;/** * Created by hp on 2016/6/10. */public class IllnessListFragment  extends Fragment {    @BindView(R.id.lv_illness)    ListView lv_illness;    private static final String illnesses = "{\n" +            "  \"error\": false,\n" +            "  \"results\": [\n" +            "  {\n" +            "    \"id\": \"1\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"神经外科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"2\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"功能神经外科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"3\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"心血管外科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"4\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"胸外科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"5\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"整形科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"6\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"乳腺外科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"7\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"泌尿外科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"8\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"肝胆外科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"9\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"肛肠科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"10\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"血管外科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"11\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"微创外科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"12\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"普外科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"13\",\n" +            "    \"city_id\":\"1\",\n" +            "    \"illness_name\":\"器官移植\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"14\",\n" +            "    \"illness_name\":\"1\",\n" +            "    \"illness_name\":\"综合外科\"\n" +            "  },\n" +            "  {\n" +            "    \"id\": \"15\",\n" +            "    \"branch_id\":\"1\",\n" +            "    \"illness_name\":\"普通内科\"\n" +            "  }\n" +            "]\n" +            "}";    List
list_illness = new ArrayList
(); private IllnessAdapter illnessAdapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_illness_list, container, false); ButterKnife.bind(this, view); initData(); return view; } private void initData() { Gson gson = new Gson(); Type type = new TypeToken
>>() { }.getType(); ResponseResult
> result = gson.fromJson(illnesses, type); list_illness = result.getResults(); illnessAdapter = new IllnessAdapter(getActivity(), list_illness); lv_illness.setAdapter(new IllnessAdapter(getActivity(), list_illness)); lv_illness.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView
parent, View view, int position, long id) { Intent intent = new Intent(getActivity(), DoctorListActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable("illness", list_illness.get(position)); intent.putExtras(bundle); startActivity(intent); } }); } public void notifyDataChange(int id) { illnessAdapter.notifyDataSetChanged(); lv_illness.setSelection(0); }}

 

 

 

转载于:https://my.oschina.net/u/2493156/blog/689612

你可能感兴趣的文章
WPF的ComboBox简单用法
查看>>
HTTP协议具体解释
查看>>
解决Android Graphical Layout 界面效果不显示
查看>>
支持FreeMarker需要哪些JAR包?
查看>>
DataTables warning : Requested unknown parameter '5' from the data source for row 0
查看>>
android studio上代码编译调试中遇到的一些异常记录
查看>>
HDFS 安全模式的理解
查看>>
Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题
查看>>
OTL翻译(7) -- otl_exception类
查看>>
hashmap理解总结
查看>>
Linux环境安装MySQL数据库(RPM格式的软件包)
查看>>
Android从源码看ListView的重用机制
查看>>
【iCore4 双核心板_ARM】例程三十三:SD_IAP_ARM实验——更新升级STM32
查看>>
Hadoop Streaming Made Simple using Joins and Keys with Python « All Things Hadoop
查看>>
二手房中介带看技巧
查看>>
非归档数据文件offline的恢复
查看>>
《裸辞的程序猿漂流记十三》——奔跑在逆袭的路上
查看>>
第三范式
查看>>
获取MS SQL TABLE列名列表
查看>>
shell常用命令集合
查看>>