看了魏小翔做的用两个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; Listlist_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" + "}"; Listlist_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); }}