如何为链接到新片段的列表视图实现onclick侦听器

时间:2017-03-29 17:23:58

标签: android listview fragment onclicklistener

所以我有一个有效的listview,但我想为这个listview实现一个onclick监听器。我希望onclick侦听器链接到一个新片段,以便在单击列表中的某个索引时打开另一个页面。我做了一些测试,并确定整个onclick监听器不起作用。

这是我到目前为止所做的:

package ca.queensu.engsoc.events;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class eventList extends Activity implements AdapterView.OnItemClickListener
{
public static final String KEY_NAME = "NAME_KEY";
ListView events;
String[] e;
mockData myBadData = new mockData();
@Override

protected void onCreate (Bundle savedInstanceState)
{
    //Fragment fragment_blank2=new SomeFragment();

    e = myBadData.getData();

    super.onCreate(savedInstanceState);

    setContentView(R.layout.event_list);

    events=(ListView) findViewById(R.id.dayList);

    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,e);

    events.setAdapter(adapter);

    events.setOnItemClickListener(this);
}

@Override

public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l)
{
    //TextView temp=(TextView) view;

    View temp=view;

    //final Toast toast = Toast.makeText(this, temp.getText() + "" + i, Toast.LENGTH_SHORT.show());

    temp.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Bundle bundle = new Bundle();

            bundle.putString(KEY_NAME,myBadData.getOneData(i));

            Fragment fr = new event_description();

            fr.setArguments(bundle);

            FragmentManager fm = getFragmentManager();

            FragmentTransaction fragmentTransaction = fm.beginTransaction();

            int contId = v.getId();

            fragmentTransaction.replace(contId, fr);

            fragmentTransaction.commit();

            Intent i=new Intent(eventList.this, fr.getClass());

        }
    });
}

}

我做错了什么?

2 个答案:

答案 0 :(得分:0)

实际上据我所知,你不需要设置任何额外的点击监听器。 onItemClick()方法应该做的东西。 final int i如果需要,这是点击视图的位置。

答案 1 :(得分:0)

要首先执行您想要的操作,您需要声明一个接口,以用作适配器和活动/片段之间的回调。

RecyclerItemCallback:

public interface RecyclerItemCallback {

    void onClick(int position);

}

然后让你的活动实现这个界面:(

public class EventList extends Activity implements RecyclerItemCallback {

    ...

    @Override
    public void onClick(int position) {
        //Implement the method "Fragment getFragment(int position)" to find the Fragment you want based on the position
        YourFragment newFragment = getFragment(position);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (addToBackStack) {
            fragmentTransaction.addToBackStack(newFragment.getClass().getSimpleName());
        }
        fragmentTransaction.replace(R.id.content, newFragment).commitAllowingStateLoss();
    }
}

然后在适配器中执行以下操作:

YourAdapter

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

    private RecyclerItemCallback recyclerCallback;
    private ArrayList<YourObject> list;
    private Context context;
    private int lastPosition;

    public NewsAdapter(Context context, RecyclerItemCallback callback) {
        this.context = context;
        this.newsList = new ArrayList<>();
        this.recyclerCallback = callback;
    }
    ...


    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView text1;
        ...
        private View.OnClickListener onRowClicked = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            recyclerCallback.onItemClick(getAdapterPosition());
        }
    };

    MyViewHolder(View itemView, int viewType) {
        super(itemView);
        this.viewType = viewType;
        ...
        itemView.setOnClickListener(onRowClicked);
    }
    ...
}