2019-08-23
1·FragmentActivity是什么
源码解释
FragmentActivity is a special activity provided in the Support Library to handle fragments on system versions older than API level 11. If the lowest system version you support is API level 11 or higher, then you can use a regular Activity.
意思就是低版本的Android中,fragmentActivity可以直接当activity使用。
在项目中,编写BaseRxFragment时,使用了FragmentActivity。
@Override public void onAttach(Context context) { super.onAttach(context); this.mActivity = (FragmentActivity) context; }
还有
protected FragmentActivity getFragmentActivity() { return mActivity; }
这样写增强了fragment的复用性,方便调用fragment所在的activity。从而实现了与fragment中控件的交互,对Activity造成影响。
2·ListView中,对Item的删除图标点击,如何传给ListView
编写回调接口来获取item里的icon。首先在Adapter里创建InnerCallback;然后在getView中,选择需要设置监听的空间,进行setOnClickListener(new ...);然后把InnerClickListener的点击事件innerclick放到onClick中,此时可以传入必要的参数,比如position,在adapter中设置setInnerClickListener;在fragment中implement这个InnerClickListener,然后让adapter.setInnerClickListen,在fragment中进行overwrite方法innerclick。代码如下:
MyDronesFragment.java
package com.qifeizn.android.fragment; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.qifeizn.android.R; import com.qifeizn.android.adapter.DroneListAdapter; import com.qifeizn.android.listener.InnerClickListener; import com.qifeizn.android.view.DroneItem; import java.util.ArrayList; public class MyDronesFragment extends BaseRxFragment implements View.OnClickListener, InnerClickListener, AdapterView.OnItemClickListener { private ListView mListView; private ArrayList<DroneItem> mDroneList = new ArrayList<>(); private DroneListAdapter mDroneListAdapter; @Override protected void initData() { DroneItem drone1 = new DroneItem("A161900050", R.drawable.v2_icon_135, "201905121314"); DroneItem drone2 = new DroneItem("A161900051", R.drawable.v2_icon_136, "201905121314"); DroneItem drone3 = new DroneItem("A161900052", R.drawable.v2_icon_137, "201905121314"); mDroneList.add(drone1); mDroneList.add(drone2); mDroneList.add(drone3); } @Override protected void initEvent() { } @Override protected void initView() { initToolbar("我的飞机"); mListView = (ListView) mView.findViewById(R.id.ls_drone_list); mDroneListAdapter = new DroneListAdapter(getContext(), R.layout.item_drone_info, mDroneList); mDroneListAdapter.setInnerClickListener(this); mListView.setAdapter(mDroneListAdapter); mListView.setOnItemClickListener(this); mView.findViewById(R.id.tv_add_drone).setOnClickListener(this); } @Override protected int getLayoutId() { return R.layout.fragment_my_drone; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_add_drone: addFragment(R.id.fl_info_content,new NewDroneFragment()); break; } } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } @Override public void innerClick(int pos) { mDroneList.remove(pos); mDroneListAdapter.notifyDataSetChanged(); } }
DroneListAdapter.ja
package com.qifeizn.android.adapter; import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.qifeizn.android.R; import com.qifeizn.android.listener.InnerClickListener; import com.qifeizn.android.view.DroneItem; import java.util.List; public class DroneListAdapter extends BaseAdapter { private List<DroneItem> mList; private LayoutInflater mLayoutInflater; private int mLayoutId; private InnerClickListener mInnerClickListener; private Context mContext; public DroneListAdapter(@NonNull Context context, int layoutId, @NonNull List<DroneItem> objects) { mLayoutInflater = LayoutInflater.from(context); mLayoutId = layoutId; mList = objects; mContext = context; } @Override public int getCount() { return mList == null ? 0 : mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } public List<DroneItem> getDroneList(){ return mList; } @NonNull @Override public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) { View view = mLayoutInflater.inflate(mLayoutId, null); DroneItem droneItem = (DroneItem) getItem(position); TextView tv_id = (TextView) view.findViewById(R.id.tv_drone_id); TextView tv_date = (TextView) view.findViewById(R.id.tv_drone_made_date); ImageView im_drone = (ImageView) view.findViewById(R.id.iv_drone_img); tv_id.setText(droneItem.getDroneId()); tv_date.setText(droneItem.getMadeDate()); im_drone.setImageResource(droneItem.getResourceId()); view.findViewById(R.id.rl_delete_drone).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mInnerClickListener.innerClick(position); } }); return view; } public void setInnerClickListener(InnerClickListener innerClickListener){ mInnerClickListener = innerClickListener; } }