基本上,我有这个结构:
活动 - > RecyclerView适配器 - >自定义视图
以下是我的代码(我已经将一些部分留下来以使其更清晰)。
这是我的Activity,MainActivity.java:
public class MainActivity extends AppCompatActivity {
private final String TAG = "MainActivity";
private EditText textField;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
textField = (EditText) findViewById(R.id.textField);
// Adapter code
}
}
这是我的适配器,MyAdapter.java:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private static final String TAG = "MyAdapter";
private Context context;
public MyAdapter(Context context) {
this.context = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View v) {
super(v);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
CustomView customView = new CustomView(context);
ViewHolder viewHolder = new ViewHolder(customView);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
//
}
}
这是我的自定义视图,CustomView.java:
public class CustomView extends RelativeLayout {
private final String TAG = "CustomView";
private RelativeLayout mLayout;
private ImageView mPicture;
public CustomView(Context context) {
super(context);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
inflate(getContext(), R.layout.item_layout, this);
this.mLayout = (RelativeLayout) findViewById(R.id.layout);
this.mPicture = (ImageView) findViewById(R.id.picture);
mPicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// change text of EditText in Activity here
}
});
}
}
我希望能够做的是,如果用户在自定义视图中点击mPicture
,它应该将我的活动中的EditText文本更改为&#34; Hello,world!&#34 ;。
我该怎么做?
答案 0 :(得分:1)
添加界面。
public class CustomView extends RelativeLayout {
public interface PictureClickListener {
public void onPictureClick();
}
private PictureClickListener listener;
public setPictureClickListener(PictureClickListener listener) {
this.listener = listener
}
...
mPicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (CustomView.this.listener != null)
CustomView.this.listener.onPictureClick();
}
});
设置界面并实现它
public class MainActivity extends AppCompatActivity
implements PictureClickListener {
@Override public void onPictureClick() {
// change text of EditText in Activity here
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Apapter code
adapter.setPictureClickListener(MainActivity.this);
}
注意:如果没有CustomView
,这会更容易......只需让ViewHolder
充气R.layout.item_layout
并将界面移到那里。
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(....); // here
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mPicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (MyAdapter.this.listener != null)
MyAdapter.this.listener.onPictureClick();
}
});
}
答案 1 :(得分:1)
首先,您可以在onCreateViewHolder
方法中膨胀自定义视图,而不是创建CustomView
类。我对此问题的解决方案是将适配器更改为abstract class
并在适配器内创建新的abstract
方法。您所要做的只是override
您活动中的abstract
方法。
//change adapter to abstract class
public abstract class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private static final String TAG = "MyAdapter";
List<String> list = new ArrayList<>(); //your data
private Context context;
public MyAdapter(Context context) {
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View customView = inflater.inflate(R.layout.item_layout, parent, false); //inflate the custom view
ViewHolder viewHolder = new ViewHolder(customView);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
ViewHolder body = (ViewHolder) holder;
body.populateView(); //you can also pass some parameters here
}
@Override
public int getItemCount() {
return list.size(); //size of your array
}
public class ViewHolder extends RecyclerView.ViewHolder {
private RelativeLayout mLayout;
private ImageView mPicture;
public ViewHolder(View v) {
super(v);
this.mLayout = (RelativeLayout) v.findViewById(R.id.layout);
this.mPicture = (ImageView) v.findViewById(R.id.picture);
}
public void populateView(){
//set click listener to the picture
this.mPicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onPictureClick(); // call the abstract method
}
});
}
}
//create new abstract method
public abstract void onPictureClick();
}
在您的活动(或片段)中:
MyAdapter adapter = new MyAdapter(getApplicationContext()) {
@Override
public void onPictureClick() { //override the abstract method
//change edittext value here
textField.setText("Hello World");
}
};
答案 2 :(得分:0)
将textField从private更改为:
public static EditText textField;
在您的CustomView中,您应该能够立即设置文本:
mPicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.textField.setText("Hello World");
}
});