frgament之间传值,list点击变颜色

直接上代码:

activity:

public class MainActivity extends FragmentActivity {

    private ContextFragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager manager = getSupportFragmentManager();
        fragment = (ContextFragment) manager.findFragmentById(R.id.fragment);

    }

    @Override
    protected void onResume() {
        super.onResume();
        WIFIUtlis.checkNetworkState(this);
    }
//写一个方法来关联两个fragment
    public void chuanzhi(int one, int two){
        fragment.setText(one, two);
    };

}
fragment1:

public class ItemFragment extends Fragment {

    private ListView mListView;
    private HashMap<String, Boolean> map;
    private ArrayList<String> list;
    private LeftAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.left_frament, null);
        mListView = (ListView) view.findViewById(R.id.left_list);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        map = new HashMap<>();

        list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            String item = "条目" + (i+1);
            list.add(item);
            map.put(item, false);
        }
        adapter = new LeftAdapter(getActivity(), list, map);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < list.size(); i++) {
                    map.put(list.get(i), i == position ? true : false);
                }
                MainActivity activity = (MainActivity) getActivity();
                //点击一个条目显示俩个条数据
                int pos = (position + 1)*2;
                activity.chuanzhi(pos-1, pos-2);
                adapter.notifyDataSetChanged();
            }
        });

    }
}
frgament2:

public class ContextFragment extends Fragment {

    private ListView mListView1;
    private List<NewsBeean.DatalistBean> mDatalist;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.context_frament, null);
        mListView1 = (ListView) view.findViewById(R.id.tv_chuan_zhi);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        XUtils_util.getInstance().getCache(Url.url, null, true, 99999999, new XUtils_util
                .XCallBack() {


            @Override
            public void onResponse(String result) {
                Gson gson = new Gson();
                NewsBeean newsBeean = gson.fromJson(result, NewsBeean.class);
                mDatalist = newsBeean.getDatalist();
            }
            @Override
            public void onFail(String result) {

            }
        });

    }
    //从新写一个集合来放入俩条数据
    public void setText(int one, int two) {
        ArrayList<NewsBeean.DatalistBean> list = new ArrayList<>();
        list.add(mDatalist.get(one));
        list.add(mDatalist.get(two));
        MyAdapter adapter = new MyAdapter(getActivity(), list);
        mListView1.setAdapter(adapter);

    }
}
第一个fragment的适配器

public class LeftAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<String> list;
    private ViewHolder mHolder;
    private HashMap<String, Boolean> map;

    public LeftAdapter(Context context, ArrayList<String> list, HashMap<String, Boolean> map) {
        mContext = context;
        this.list = list;
        this.map = map;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            mHolder = new ViewHolder();
            convertView=View.inflate(mContext,R.layout.layout_left,null);
            mHolder.title= (TextView) convertView.findViewById(R.id.te);
                convertView.setTag(mHolder);
        }else {
            mHolder= (ViewHolder) convertView.getTag();
        }
        String text = list.get(position);
        mHolder.title.setSelected(map.get(text));
        mHolder.title.setText(text);
        return convertView;
    }
    class ViewHolder{
        TextView title;
    }
}
给点击条目的内容设置背景颜色
<?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">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="20sp"
    android:background="@drawable/selset"
    android:id="@+id/te"/>
</LinearLayout>
//颜色选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorAccent" android:state_selected="true"></item>
    <item android:drawable="@color/bai"></item>
</selector>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值