小编Far*_*eed的帖子

处理Recyclerview内部物品的触摸事件 - android

我正在使用自定义布局(文本视图和切换)和自定义适配器构建简单的回收站视图.

我的问题是我无法处理交换机的点击事件(这使其切换状态),我已经为RecyclerView它构建了一个触摸监听器,每次点击它都会触发Switch.

经过大量试验后,开关触摸监听器也会触发,但如果单击该开关,我想禁用recyclelerView的触摸事件.

这是我的布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60.0dip"
android:layout_weight="1.0"
android:orientation="horizontal"
android:paddingLeft="16dp">

<TextView
    android:id="@+id/category_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="false"
    android:layout_centerVertical="true"
    android:layout_gravity="center_vertical"
    android:text="Category Name"
    android:textAppearance="?android:textAppearanceLarge" />

<android.support.v7.widget.SwitchCompat
    android:id="@+id/category_switch"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_gravity="center_vertical"
    android:checked="true"
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:paddingRight="16dp" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是我的适配器代码:

public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View row = layoutInflater.inflate(R.layout.single_row_category, parent, false);
    CategoryViewHolder holder = new CategoryViewHolder(row);

    return holder;
}

public void onBindViewHolder(CategoryViewHolder holder, final int position) {
    AlarmCategory thisCat = categories.get(position);

    holder.catName.setText(thisCat.getCategoryName());

    holder.catStatus.setChecked(thisCat.isOn());

    holder.catStatus.setOnTouchListener(new View.OnTouchListener() …
Run Code Online (Sandbox Code Playgroud)

android touch-event android-switch android-recyclerview

12
推荐指数
2
解决办法
3万
查看次数

使用bundle在片段之间传递数据到另一个片段示例

我的应用程序中有3个sherlockListFragments.每个片段都有一些editTexts,最后一个片段有一个按钮,当按下它时,应该访问和存储第一个和第二个片段中的所有数据.我使用bundle在片段之间发送数据.用以下简单示例,这是我的第一个片段的代码:

public class PersonalDataFragment extends SherlockListFragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragmet_personal_data, container, false);

    return v;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    PersonalDataFragment fragment = new PersonalDataFragment();
    Bundle bundle = new Bundle();
    bundle.putString("y", "koko"); //any string to be sent
    fragment.setArguments(bundle);

    super.onCreate(savedInstanceState);
}
Run Code Online (Sandbox Code Playgroud)

这是接收文本的片段的代码:

public class WorkExpRefFragment extends SherlockListFragment  {
String myInt;

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


    final EditText …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-listfragment

6
推荐指数
1
解决办法
2万
查看次数