Giter VIP home page Giter VIP logo

multiview's Introduction

Android九宫格显示多图或item

gradle依赖

    compile 'cn.lemon:multiview:1.0.0'

MultiView

  • 由于依赖了RestHttp加载网络图片,需初始化
  RestHttp.initialize(this);
  if(BuildConfig.DEBUG){
       RestHttp.setDebug(true,"network");
  }
  • xml布局文件
    <cn.lemon.multi.MultiView
        android:id="@+id/multi_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="#f1506d"
        app:divideSpace="8dp"
        app:placeholder="@drawable/holder"/>

设置item之间的间隔app:divideSpace="8dp" 设置占位图app:placeholder="@drawable/holder"

  • Java代码
 multiView = (multiView) findViewById(R.id.cell_view);
 multiView.setLayoutParams(new LinearLayout.LayoutParams(900, ViewGroup.LayoutParams.WRAP_CONTENT));

 data.add("http://i02.pictn.sogoucdn.com/73a90748d5e19769");
 data.add("http://i01.pictn.sogoucdn.com/e19188bbc3966d6f");

 multiView.setImages(data);   设置图片资源
  • 自定义Adapter

如果需显示的不是图片,而是复杂的item,需自定义Adapter

  • 继承MultiAdapter
    class MyAdapter extends MultiAdapter<String> {

        private TextView textView;
        public MyAdapter(Context context) {
            super(context);
        }

        @Override
        public View getView(ViewGroup parent, int position) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
            textView = (TextView) view.findViewById(R.id.text);
            return view;
        }

        @Override
        public void setData(String object) {
            super.setData(object);
            textView.setText(object);
        }

        @Override
        public void setOnItemClick() {
            super.setOnItemClick();
            Util.Toast("点击事件");
        }
    }
  • 给 MultiView 设置 MultiAdapter,并添加数据
   adapter = new MyAdapter(this);
   multiView.setAdapter(adapter);
   adapter.addAll(data);

ViewImageActivity

显示大图

  • manifests 文件中添加
   <activity android:name="cn.lemon.multi.ui.ViewImageActivity"/>
  • 虽然 ViewImageActivity 主要是联合 MultiView 使用,但是其实不管是否使用 MultiView, 同样可以使用 ViewImageActivity,只需要在跳转 Activity 的时候绑定好数据,如:
  Intent intent = new Intent(itemView.getContext(),ViewImageActivity.class);
  intent.putExtra(ViewImageActivity.IMAGES_DATA_LIST, (Serializable) getData());
  intent.putExtra(ViewImageActivity.IMAGE_NUM, getAdapterPosition());
  itemView.getContext().startActivity(intent);

注意事项

依赖的其他库

   compile 'com.android.support:appcompat-v7:23.1.1'
   compile 'com.android.support:support-v4:23.1.1'
   compile 'cn.alien95:resthttp:1.0.5'

九宫格

自定义Adapter

multiview's People

Watchers

James Cloos avatar

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.