如何在此搜索功能中添加onclicklistner

时间:2017-04-24 22:43:13

标签: java android xml

我想在此搜索功能中添加onclicklisnter,但我无法执行此操作。过滤项目后,点击项目列表不起作用。我尝试了stackoverflow上提供的许多答案,但它们对我不起作用。到目前为止,我已经尝试过 -

ExampleActivity.java

package com.tricktekno.search;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.yalantis.filter.adapter.FilterAdapter;
import com.yalantis.filter.animator.FiltersListItemAnimator;
import com.yalantis.filter.listener.FilterListener;
import com.yalantis.filter.widget.Filter;
import com.yalantis.filter.widget.FilterItem;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ExampleActivity extends AppCompatActivity implements FilterListener<Tag> {

    private RecyclerView mRecyclerView;

    private int[] mColors;
    private String[] mTitles;
    private List<Question> mAllQuestions;
    private Filter<Tag> mFilter;
    private QuestionsAdapter mAdapter;

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

        ImagePipelineConfig config = ImagePipelineConfig
                .newBuilder(this)
                .setDownsampleEnabled(true)
                .build();
        Fresco.initialize(this, config);

        mColors = getResources().getIntArray(R.array.colors);
        mTitles = getResources().getStringArray(R.array.job_titles);

        mFilter = (Filter<Tag>) findViewById(R.id.filter);
        mFilter.setAdapter(new Adapter(getTags()));
        mFilter.setListener(this);

        //the text to show when there's no selected items
        mFilter.setNoSelectedItemText(getString(R.string.str_all_selected));
        mFilter.build();


        mRecyclerView = (RecyclerView) findViewById(R.id.list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(mAdapter = new QuestionsAdapter(this, mAllQuestions = getQuestions()));
        mRecyclerView.setItemAnimator(new FiltersListItemAnimator());
    }

    private void calculateDiff(final List<Question> oldList, final List<Question> newList) {
        DiffUtil.calculateDiff(new DiffUtil.Callback() {
            @Override
            public int getOldListSize() {
                return oldList.size();
            }

            @Override
            public int getNewListSize() {
                return newList.size();
            }

            @Override
            public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
                return oldList.get(oldItemPosition).equals(newList.get(newItemPosition));
            }

            @Override
            public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
                return oldList.get(oldItemPosition).equals(newList.get(newItemPosition));
            }
        }).dispatchUpdatesTo(mAdapter);
    }

    private List<Tag> getTags() {
        List<Tag> tags = new ArrayList<>();

        for (int i = 0; i < mTitles.length; ++i) {
            tags.add(new Tag(mTitles[i], mColors[i]));
        }

        return tags;
    }

    @Override
    public void onNothingSelected() {
        if (mRecyclerView != null) {
            mAdapter.setQuestions(mAllQuestions);
            mAdapter.notifyDataSetChanged();
        }
    }

    private List<Question> getQuestions() {
        return new ArrayList<Question>() {{
            add(new Question("Carol Bell", "Graphic Designer",
                    "http://kingofwallpapers.com/girl/girl-011.jpg", "Nov 20, 6:12 PM",
                    "What is the first step to transform an idea into an actual project?", new ArrayList<Tag>() {{
                add(new Tag(mTitles[2], mColors[2]));
                add(new Tag(mTitles[4], mColors[4]));
            }}));
            add(new Question("Melissa Morales", "Project Manager",
                    "http://weknowyourdreams.com/images/girl/girl-03.jpg", "Nov 20, 3:48 AM",
                    "What is your biggest frustration with taking your business/career (in a corporate) to the next level?", new ArrayList<Tag>() {{
                add(new Tag(mTitles[1], mColors[1]));
                add(new Tag(mTitles[5], mColors[5]));

            }}));
            add(new Question("Rochelle Yingst", "iOS Developer",
                    "http://www.viraldoza.com/wp-content/uploads/2014/03/8876509-lily-pretty-girl.jpg", "Nov 20, 6:12 PM",
                    "What is the first step to transform an idea into an actual project?", new ArrayList<Tag>() {{
                add(new Tag(mTitles[7], mColors[7]));
                add(new Tag(mTitles[8], mColors[8]));
            }}));
            add(new Question("Lacey Barbara", "QA Engineer",
                    "http://kingofwallpapers.com/girl/girl-019.jpg", "Nov 20, 6:12 PM",
                    "What is the first step to transform an idea into an actual project?", new ArrayList<Tag>() {{
                add(new Tag(mTitles[3], mColors[3]));
                add(new Tag(mTitles[9], mColors[9]));

            }}));
            add(new Question("Teena Allain", "Android Developer",
                    "http://tribzap2it.files.wordpress.com/2014/09/hannah-simone-new-girl-season-4-cece.jpg", "Nov 20, 6:12 PM",
                    "What is the first step to transform an idea into an actual project?", new ArrayList<Tag>() {{
                add(new Tag(mTitles[1], mColors[1]));
                add(new Tag(mTitles[6], mColors[6]));
            }}));
        }};

    }



    public void details(View v){
        TextView tv =(TextView) findViewById(R.id.text_name);
        tv.setText("Anurag");
        tv.setTextColor(Color.GREEN);
    }



    private List<Question> findByTags(List<Tag> tags) {
        List<Question> questions = new ArrayList<>();

        for (Question question : mAllQuestions) {
            for (Tag tag : tags) {
                if (question.hasTag(tag.getText()) && !questions.contains(question)) {
                    questions.add(question);
                }
            }
        }

        return questions;
    }

    @Override
    public void onFiltersSelected(@NotNull ArrayList<Tag> filters) {
        List<Question> newQuestions = findByTags(filters);
        List<Question> oldQuestions = mAdapter.getQuestions();
        mAdapter.setQuestions(newQuestions);
        calculateDiff(oldQuestions, newQuestions);
    }

    @Override
    public void onFilterSelected(Tag item) {
        if (item.getText().equals(mTitles[0])) {
            mFilter.deselectAll();
            mFilter.collapse();
        }
    }

    @Override
    public void onFilterDeselected(Tag item) {

    }

    class Adapter extends FilterAdapter<Tag> {

        Adapter(@NotNull List<? extends Tag> items) {
            super(items);
        }

        @NotNull
        @Override
        public FilterItem createView(int position, Tag item) {
            FilterItem filterItem = new FilterItem(ExampleActivity.this);

            filterItem.setStrokeColor(mColors[0]);
            filterItem.setTextColor(mColors[0]);
            filterItem.setCheckedTextColor(ContextCompat.getColor(ExampleActivity.this, android.R.color.white));
            filterItem.setColor(ContextCompat.getColor(ExampleActivity.this, android.R.color.white));
            filterItem.setCheckedColor(mColors[position]);
            filterItem.setText(item.getText());
            filterItem.deselect();

            return filterItem;
        }
    }

}

activit_example.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:elevation="4dp"
            android:paddingRight="16dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.v7.widget.AppCompatImageView
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_centerVertical="true"
                    android:src="@drawable/ic_alarm"
                    android:tint="@android:color/white" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="Questions"
                    android:textColor="@android:color/white"
                    android:textSize="20sp" />

                <android.support.v7.widget.AppCompatImageView
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/ic_search"
                    android:tint="@android:color/white" />

            </RelativeLayout>

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#E4E6E3"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onItemClick"
            android:clickable="true"
            android:layout_marginTop="@dimen/container_height"
            tools:listitem="@layout/item_list" />

        <com.yalantis.filter.widget.Filter
            android:id="@+id/filter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </FrameLayout>
</android.support.design.widget.CoordinatorLayout>

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="onItemClick"
        android:clickable="true">

    <View
        android:id="@+id/dividerTop"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="#E4E6E3" />

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/avatar"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_below="@+id/dividerTop"
        android:layout_margin="16dp"
        fresco:actualImageScaleType="focusCrop"
        fresco:roundAsCircle="true"
        fresco:roundWithOverlayColor="@android:color/white" />

    <TextView
        android:id="@+id/text_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/avatar"
        android:layout_marginTop="10dp"
        android:layout_toEndOf="@+id/avatar"
        android:layout_toLeftOf="@+id/text_date"
        android:layout_toStartOf="@+id/text_date"
        android:text="Carol Bell"
        android:textColor="#827f93"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/text_job_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/text_name"
        android:layout_below="@+id/text_name"
        android:layout_marginBottom="10dp"
        android:text="Graphic Designer"
        android:textColor="#827f93" />

    <TextView
        android:id="@+id/text_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/dividerTop"
        android:layout_margin="26dp"
        android:text="Nov 20, 6:12 PM"
        android:textColor="#827f93" />

    <TextView
        android:id="@+id/filter_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/avatar"
        android:layout_marginLeft="16dp"
        android:padding="10dp"
        android:textColor="@android:color/white" />

    <TextView
        android:id="@+id/filter_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/filter_first"
        android:layout_marginLeft="10dp"
        android:layout_toEndOf="@+id/filter_first"
        android:padding="10dp"
        android:textColor="@android:color/white" />

    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/text_question"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:background="#ebebeb" />

    <TextView
        android:id="@+id/text_question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/filter_first"
        android:layout_margin="16dp"
        android:text="What is the first step to transform an idea into an actual project?"
        android:textColor="#827f93" />

    </RelativeLayout>

</RelativeLayout>

0 个答案:

没有答案