GithubHelp home page GithubHelp logo

labelview's Introduction

HotWordView

a ui widget for hot word

How it looks

How to use

HotWordListView It's easy to use like Listview.

you can set custom attributes as shown below:

<declare-styleable name="HotWordListView">
        <attr name="vertical_gap" format="dimension"></attr>
        <attr name="horizontal_gap" format="dimension"></attr>
</declare-styleable>

a example:

activity_main.xml:

<com.monster.library.HotWordListView
        android:id="@+id/search_hot_word_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:horizontal_gap="16dp"
        app:vertical_gap="16dp"
        android:padding="16dp"/>

MainActivity.java

public class MainActivity extends AppCompatActivity implements HotWordListView.OnItemClickListener{

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

        initView();
    }

    private void initView() {
        List<HotWordBean> testData = createTestData();
        mHotWordView = findViewById(R.id.search_hot_word_view);
        mAdapter = new HotWordAdapter(this, testData);
        mHotWordView.setAdapter(mAdapter);
        mHotWordView.setOnItemClickListener(this);
    }
    
    @Override
    public void onItemClick(View view, int position) {
        if (mAdapter == null) {
            return;
        }
        HotWordBean hotWordBean = (HotWordBean) mAdapter.getItem(position);
        if (hotWordBean == null) {
            return;
        }
        Toast.makeText(this, hotWordBean.getWord(), Toast.LENGTH_SHORT).show();
    }
}

Adapter

class HotWordAdapter extends BaseAdapter {

        private Context mContext;
        private List<HotWordBean> mList;

        public HotWordAdapter(Context context, List<HotWordBean> list) {
            mContext = context;
            mList = list;
        }

        @Override
        public int getCount() {
            return mList == null ? 0 : mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList == null ? null : mList.get(position);
        }

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

        @Override
        public int getItemViewType(int position) {
            return mList.get(position) == null ? 0 : mList.get(position).getType();
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            TextView textView;
            switch (getItemViewType(position)) {
                case RED:
                    textView = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_hot_word_red, null);
                    break;
                case NORMAL:
                default:
                    textView = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_hot_word, null);
                    break;
            }
            HotWordBean bean = mList.get(position);
            textView.setText(bean.getWord());
            return textView;
        }
    }

For more, please turn to the source code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.