繁体   English   中英

从自定义视图交流到活动

[英]Communicate from custom view to activity

我有一个看起来像这样的结构:

Activity -> Adapter -> Custom View

在活动的布局中,我有一个EditText 在自定义视图的布局中,我有一个Button 我需要这样做,以便当用户单击自定义视图中的按钮时,它会集中在活动中的EditText上,以便用户可以键入消息。

我怎样才能做到这一点?

这是我的活动:

public class MainActivity extends AppCompatActivity {    
    private List<City> cities;

    private MyAdapter adapter;

    private RecyclerView.LayoutManager layoutManager;

    private RecyclerView recyclerView;

    // The activity's EditText
    private EditText editText;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        editText = (EditText) findViewById(R.id.editText);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        adapter = new MyAdapter(context, cities, true);
        recyclerView.setAdapter(adapter);
    }
}

这是我的适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private Context context;

    private List<City> cities;

    public MyAdapter(Context context, List<City> cities) {
        this.context = context;
        this.cities = cities;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public City city;

        public ViewHolder(CustomCityView itemView) {
            super(itemView);
        }

        public void setCity(City city) {
            ((CustomCityView) itemView).setCity(city);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CustomCityView customView = new CustomCityView(context);

        return new ViewHolder(customView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        City city = cities.get(position);

        holder.setCity(city);
    }

    @Override
    public int getItemCount() {
        return cities.size();
    }
}

最后,这是我的自定义视图类:

public class CustomCityView extends RelativeLayout {
    private City city;

    private TextView cityName;

    // The button mentioned above
    private Button button;

    public CustomCityView(Context context) {
        super(context);
        init();
    }

    public CustomCityView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.custom_city_layout, this, true);

        cityName = (TextView) findViewById(R.id.cityName);
        button = (Button) findViewById(R.id.button);
    }

    public void setCity(final City city) {
        this.city = city;

        cityName.setText(city.getName());

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // If the user clicks on this button, how do I focus on the activity's EditText (and pass data to it)?
            }
        });
    }
}

这是自定义视图的布局:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/cityName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

问题很简单。 如果用户单击自定义视图的Button ,如何在活动中聚焦于EditText ,以便用户可以键入消息?

此外,是否可以将数据从自定义视图(例如,自定义布局中的城市名称)传递到活动,以便在活动的EditText中将其显示为“提示”?

遵循与View.OnClickListener相同的逻辑...

只需定义您自己的单击界面即可。

我们称之为CustomCityView.CityButtonClickListener

public class CustomCityView extends RelativeLayout 
    implements View.OnClickListener {

    public interface CityButtonClickListener {
        void onCityButtonClick(City city);
    }

    private CityButtonClickListener buttonClickListener;

    public void setCityButtonClickListener(CityButtonClickListener listener) {
        this.buttonClickListener = listener;
    }

    ...

然后,在常规onClick致电给您。

public class CustomCityView extends RelativeLayout 
    implements View.OnClickListener {

    ...

    @Override 
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                if (buttonClickListener != null) {
                    buttonClickListener.onCityButtonClick(city);
                }
                break;
            default:
                break;
        }
    }

    public void setCity(final City city) {
        this.city = city;

        cityName.setText(city.getName());

        button.setOnClickListener(this);
    }

然后,从实现Activity类的接口向下传递到Adapter / ViewHolder。

换句话说,您可以投放上下文

    public void setCity(City city) {
        CustomCityView cityView = (CustomCityView) itemView;
        cityView.setCity(city);
        try {
            cityView.setCityButtonClickListener( (CityButtonClickListener) context);
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
    }

回到活动...

public class MainActivity extends AppCompatActivity 
    implements CustomCityView.CityButtonClickListener { 

    @Override 
    public void onCityButtonClick(City city) {
        // TODO: Update EditText
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM