看了魏小翔做的用两个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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#35b4c2">
<ImageButton
android:id="@+id/ib_back"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:background="@null"
android:src="@drawable/back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="找医生"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="1">
<fragment
android:id="@+id/fg_dept"
android:name="com.mialab.healthbutler.fragment.BranchListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
<fragment
android:id="@+id/fg_illness"
android:name="com.mialab.healthbutler.fragment.IllnessListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
</LinearLayout>
- 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<Branch> list_branchs = new ArrayList<Branch>();
@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<ResponseResult<List<Branch>>>() {
}.getType();
ResponseResult<List<Branch>> 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<Illness> list_illness = new ArrayList<Illness>();
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<ResponseResult<List<Illness>>>() {
}.getType();
ResponseResult<List<Illness>> 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);
}
}