ClickListener从适配器到其他类

时间:2017-05-08 19:11:52

标签: android recycler-adapter

如何使用界面创建clickListener?它与数据库后台的加载数据完美配合。但是,当我想在其他类中创建监听器时,MapsActivity无法正常工作

我的主要活动:

public class MapsActivity extends FragmentActivity implements LoadAllClubsInterface {

    private DrawerLayout drawerLayout;

    private RecyclerView clubRecycler;
    private RecyclerView.LayoutManager clubLayoutManager;
    private ArrayList<ClubBean> clubList = new ArrayList<ClubBean>();

    private RecyclerView.Adapter clubAdapter;

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

        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().replace(R.id.content_frame, new MyMapFragment()).commit();

        new LoadAllClubs(this).execute(); //load list in background from database

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        clubRecycler = (RecyclerView) findViewById(R.id.recycler_view);
        clubRecycler.setHasFixedSize(true);
        clubLayoutManager = new LinearLayoutManager(this);

        clubRecycler.setLayoutManager(clubLayoutManager);

    }

    @Override
    public void finishDataLoad(ArrayList<HashMap<String, String>> clubs) {
        Iterator<HashMap<String, String>> iterator = clubs.iterator();
        Map<String, String> map = new HashMap<String, String>();
        while (iterator.hasNext()){
            map = iterator.next();
            clubList.add(new ClubBean(map.get("name"),map.get("localization"), map.get("score")));
        }
        clubAdapter = new ClubAdapter(clubList, this);
        clubRecycler.setAdapter(clubAdapter);
    }


}

我的适配器类:

public class ClubAdapter extends RecyclerView.Adapter<ClubAdapter.MyViewHolder> {

    private ArrayList<ClubBean> clubList = new ArrayList<ClubBean>();
    private OnClubClickListener onClubListClickListener;
    Context mContext;

    public ClubAdapter(ArrayList<ClubBean> clubList, Context mContext){
        this.clubList = clubList;
        this.mContext = mContext;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CardView cv = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.club_view_item, parent, false);
        MyViewHolder myHolder = new MyViewHolder(cv);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final ClubBean club = clubList.get(position);
        CardView cardView = holder.cardView;
        TextView textClubName = (TextView) cardView.findViewById(R.id.club_name);
        TextView textClubLoc = (TextView) cardView.findViewById(R.id.club_loc);
        TextView textClubRating = (TextView) cardView.findViewById(R.id.club_rating);

        textClubName.setText(club.getClubName());
        textClubLoc.setText(club.getClubLoc());
        textClubRating.setText(club.getClubRating());

        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View v) {
                onClubListClickListener.onClicked(club);
            }
        });
    }

    public interface OnClubClickListener{
        void onClicked(ClubBean club);
    }
    public void setOnClickListener(OnClubClickListener onClubListClickListener) {
        this.onClubListClickListener = onClubListClickListener;
    }
    @Override
    public int getItemCount() {
        return clubList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        private CardView cardView;

        public MyViewHolder(CardView v){
            super(v);
            cardView = v;
        }
    }
}

我的地图片段:

public class MyMapFragment extends Fragment implements OnMapReadyCallback, ClubAdapter.OnClubClickListener{

    GoogleMap mGoogleMap;
    MapView mMapView;
    View mView;

    public MyMapFragment(){

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.map_fragment, container, false);

        return mView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mMapView = (MapView) mView.findViewById(R.id.map);
        if (mMapView != null){
            mMapView.onCreate(null);
            mMapView.onResume();
            mMapView.getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(getActivity().getApplicationContext());
        mGoogleMap = googleMap;

        LatLng city = new LatLng(10.4158773,10.6337789);
        mGoogleMap.addMarker(new MarkerOptions().position(city).title("city"));
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(city, 11));
    }


    @Override
    public void onClicked (ClubBean club) {
        ClubBean bean = club;
        Log.d("Name: ", bean.getClubName());
    }
}

1 个答案:

答案 0 :(得分:0)

这里实际上存在一些误解。在您的代码中,您正在RecyclerView内创建并填充MapsActivity,并且您正在OnClubClickListener中实施MyMapFragment,这样做无效。要解决此问题,您必须在OnClubClickListener内实施MapsActivity,或将RecyclerViewAdapter的创建更改为MyMapFragment

完成此操作后,您必须使用Adapter将界面实例传递给Constructor

<强> MapsActivity

public class MapsActivity extends FragmentActivity implements LoadAllClubsInterface {

    private DrawerLayout drawerLayout;

    private RecyclerView clubRecycler;
    private RecyclerView.LayoutManager clubLayoutManager;
    private ArrayList<ClubBean> clubList = new ArrayList<ClubBean>();

    private RecyclerView.Adapter clubAdapter;

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

        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().replace(R.id.content_frame, new MyMapFragment()).commit();

        new LoadAllClubs(this).execute(); //load list in background from database

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        clubRecycler = (RecyclerView) findViewById(R.id.recycler_view);
        clubRecycler.setHasFixedSize(true);
        clubLayoutManager = new LinearLayoutManager(this);

        clubRecycler.setLayoutManager(clubLayoutManager);

    }

    @Override
    public void finishDataLoad(ArrayList<HashMap<String, String>> clubs) {
        Iterator<HashMap<String, String>> iterator = clubs.iterator();
        Map<String, String> map = new HashMap<String, String>();
        while (iterator.hasNext()){
            map = iterator.next();
            clubList.add(new ClubBean(map.get("name"),map.get("localization"), map.get("score")));
        }
        //pass the class that implements your listener as a parameter.
        clubAdapter = new ClubAdapter(clubList, this, this);
        clubRecycler.setAdapter(clubAdapter);
    }

 @Override
    public void onClicked (ClubBean club) {
        ClubBean bean = club;
        Log.d("Name: ", bean.getClubName());
    }

}

您的适配器类:

public class ClubAdapter extends RecyclerView.Adapter<ClubAdapter.MyViewHolder> {

    private ArrayList<ClubBean> clubList = new ArrayList<ClubBean>();
    private OnClubClickListener onClubListClickListener;
    Context mContext;

    public ClubAdapter(ArrayList<ClubBean> clubList, Context mContext, OnClubClickListener listener){
        this.clubList = clubList;
        this.mContext = mContext;
        this.onClubListClickListener = listener;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CardView cv = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.club_view_item, parent, false);
        MyViewHolder myHolder = new MyViewHolder(cv);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final ClubBean club = clubList.get(position);
        CardView cardView = holder.cardView;
        TextView textClubName = (TextView) cardView.findViewById(R.id.club_name);
        TextView textClubLoc = (TextView) cardView.findViewById(R.id.club_loc);
        TextView textClubRating = (TextView) cardView.findViewById(R.id.club_rating);

        textClubName.setText(club.getClubName());
        textClubLoc.setText(club.getClubLoc());
        textClubRating.setText(club.getClubRating());

        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View v) {
                onClubListClickListener.onClicked(club);
            }
        });
    }

    public interface OnClubClickListener{
        void onClicked(ClubBean club);
    }
    public void setOnClickListener(OnClubClickListener onClubListClickListener) {
        this.onClubListClickListener = onClubListClickListener;
    }
    @Override
    public int getItemCount() {
        return clubList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        private CardView cardView;

        public MyViewHolder(CardView v){
            super(v);
            cardView = v;
        }
    }
}