FAB& Textview作为ListViewItem

时间:2017-03-21 03:40:30

标签: android

我有一个listview,其中包含fab& amp; TextView中。 listviewitem布局代码如下:

<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">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabActiveStat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:clickable="true"
        android:focusable="false"
        app:fabSize="mini"
        app:srcCompat="@drawable/ic_done" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="0dp"
        android:layout_toEndOf="@+id/fabActiveStat"
        android:layout_toLeftOf="@+id/fabActiveStat"
        android:clickable="true"
        android:focusable="false"
        app:fabSize="mini"
        app:srcCompat="@android:drawable/ic_menu_edit" />

    <TextView
        android:id="@+id/tvDesc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/fabEdit"
        android:layout_centerVertical="true"
        android:layout_marginLeft="0dp"
        android:layout_marginStart="0dp"
        android:layout_toEndOf="@+id/fabEdit"
        android:maxLines="1"
        android:text=""
        android:textSize="17sp"
        android:clickable="true"
        android:focusable="false"
        android:textStyle="bold" />

</RelativeLayout>

以下适配器代码:

ctx = getActivity();
    // The desired columns to be bound
    AScolumns = new String[]{
            //dbHandler.COLUMN_ID,
            dbHandler.COLUMN_CATEGORY,
            //dbHandler.COLUMN_ACTIVE_STATUS
    };

    AIto = new int[]{
            //R.id.fabActiveStat,
            R.id.tvDesc,
            //R.id.fabEdit
    };

    dbHandler = new databaseHandler(ctx, null, null, 1);
    csAdapter = new SimpleCursorAdapter(ctx, R.layout.view_categ_data, null, AScolumns, AIto, 0);
    lvCategory.setAdapter(csAdapter);
    getLoaderManager().initLoader(0, null, this);

    lvCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d(TAG, "Listview di click");
        }
    });
}

当所有工厂移除并且我在项目点击工作上使用多个textview listview就好了。但是当工厂使用listview时,项目点击根本不起作用。基于我在互联网上阅读的内容,必须将焦点设置为false,但这不起作用。任何人都知道如何将fab作为列表视图项目并使textview&amp; fab clickable?

注意:我是新手

1 个答案:

答案 0 :(得分:0)

您可以使用RecyclerView。我在这里使用String模型,但您可以稍后使用自己的自定义模型。要填充列表(项目),您需要创建自定义适配器和自定义视图(就像您一样)。这是自定义适配器:

public abstract class CustomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context context;
private List<String> data;
private LayoutInflater inflater;

public CustomAdapter(Context context, List<String> data) {
    this.context = context;
    this.data = data;
    this.inflater = LayoutInflater.from(context);
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.custom_view, parent, false); //use your custom view here
    return new BodyViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    String model = data.get(position);
    BodyViewHolder body = (BodyViewHolder) holder;
    body.populate(model);
}

@Override
public long getItemId(int position) {
    return position;
}

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

class BodyViewHolder extends RecyclerView.ViewHolder {

    TextView tvDesc;
    FloatingActionButton btn_edit;
    FloatingActionButton btn_active;
    ImageView imageView;

    public BodyViewHolder(View itemView) {
        super(itemView);
        tvDesc = (TextView) itemView.findViewById(R.id.tvDesc);
        btn_active = (FloatingActionButton) itemView.findViewById(R.id. fabActiveStat);
        btn_edit = (FloatingActionButton) itemView.findViewById(R.id.fabEdit);
    }

    public void populate(final String model) {
        tvDesc.setText(model);

        btn_edit.setOnClickListener(view -> onModelClick(model));//firing listener to btn_edit, call the abstract method
    }
}

public abstract void onModelClick(ProductModel model);

}

在你的xml文件中:

<android.support.v7.widget.RecyclerView
    android:padding="5dp"
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

在您的活动中:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);

List<String> listProduct = new ArrayList<>();
CustomAdapter adapterProduct = new CustomAdapter(getApplicationContext(), listProduct) {
    @Override
    public void onModelClick(String model) {
        //your custom action
    }
};

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(adapterProduct);

//add some data to the ArrayList

adapter.notifyDataSetChanged();