android仿微信发朋友圈功能
博客专区 > executor 的博客 > 博客详情
android仿微信发朋友圈功能
executor 发表于2年前
android仿微信发朋友圈功能
  • 发表于 2年前
  • 阅读 4289
  • 收藏 8
  • 点赞 4
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

先上图片,做的很一般,但是基本实现了。

大概就这样,能实现添加图片和删除图片的功能。

代码在这里

AlbumActivity.java
public class AlbumActivity extends ToolBarActivity {

    private EditText mEditContent;
    private GridView mGridView;
    private GridViewAdapter gridViewAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album);
        showActionBarBack(true);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_album, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_publish:
                //实现发送的功能
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    @Override
    public void initView() {
        mEditContent = (EditText) findViewById(R.id.edit_content);
        mGridView = (GridView) findViewById(R.id.gridView);
        gridViewAdapter = new GridViewAdapter(this);
        mGridView.setAdapter(gridViewAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!gridViewAdapter.getData().get(position).isPick()) {
                    gridViewAdapter.removeData(position);
                } else {
                    gridViewAdapter.addData(new AlbumBean("", false));
                }
            }
        });
    }

    @Override
    public void initData() {
    }
}
GridViewAdapter.java
public class GridViewAdapter extends BaseAdapter {
    private List<AlbumBean> albumBeanList;
    private Context mContext;

    public GridViewAdapter(Context mContext) {
        albumBeanList = new ArrayList<>();
        albumBeanList.add(new AlbumBean("", true));
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return albumBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return albumBeanList.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final AlbumBean albumBean = albumBeanList.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_album_item, null);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (albumBean.isPick()) {
            viewHolder.imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.marquee_add_photo_48));
        } else {
            //
            viewHolder.imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.face_btn_normal));
        }
        return convertView;
    }

    public void addData(AlbumBean albumBean) {
        switch (albumBeanList.size()) {
            case 1:
            case 2:
            case 3:
                //先移除最后那一个,在添加
                albumBeanList.remove(albumBeanList.size() - 1);
                albumBeanList.add(albumBean);
                albumBeanList.add(new AlbumBean("", true));
                break;
            case 4:
                //先移除最后那一个,在添加
                albumBeanList.remove(albumBeanList.size() - 1);
                albumBeanList.add(albumBean);
                break;
        }
        this.notifyDataSetInvalidated();
    }

    public void removeData(int position) {
        removeData(albumBeanList.get(position));
    }

    public void removeData(AlbumBean albumBean) {
        if (albumBeanList != null && albumBeanList.contains(albumBean)) {
            //判断当前的数量
            switch (albumBeanList.size()) {
                case 1:
                case 2:
                case 3:
                    albumBeanList.remove(albumBean);
                    break;
                case 4:
                    albumBeanList.remove(albumBean);
                    if (!albumBeanList.get(albumBeanList.size() - 1).isPick()) {
                        albumBeanList.add(new AlbumBean("", true));
                    }
                    break;
            }
            this.notifyDataSetInvalidated();
        }
    }

    public List<AlbumBean> getData() {
        return albumBeanList;
    }

    private static class ViewHolder {
        public ImageView imageView;
    }
}
public class AlbumBean {
    private String filePath;
    private boolean isPick;//标识+

    public AlbumBean() {
    }

    public AlbumBean(String filePath, boolean isPick) {
        this.filePath = filePath;
        this.isPick = isPick;
    }


    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public boolean isPick() {
        return isPick;
    }

    public void setPick(boolean pick) {
        isPick = pick;
    }
}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context="org.liangxiaokou.module.album.AlbumActivity">

    <include layout="@layout/view_top_bar" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="vertical">


        <!--http://www.chengxuyuans.com/Android/79282.html-->
        <!--android:background="@android:drawable/edit_text"-->
        <EditText
            android:id="@+id/edit_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:enabled="true"
            android:gravity="top"
            android:inputType="textMultiLine"
            android:maxLines="3"
            android:minLines="3"
            android:padding="@dimen/activity_vertical_margin_10"
            android:scrollbars="vertical"
            android:textColor="@android:color/black"
            android:textSize="@dimen/normal_content_size" />

        <!--http://blog.csdn.net/ztp800201/article/details/14469883-->
        <GridView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:cacheColorHint="#00000000"
            android:columnWidth="65dp"
            android:fadingEdge="none"
            android:horizontalSpacing="2dp"
            android:listSelector="#00000000"
            android:numColumns="auto_fit"
            android:scrollbars="none"
            android:stretchMode="columnWidth"
            android:transcriptMode="alwaysScroll"
            android:verticalSpacing="2dp" />
    </LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:focusable="false"
    android:padding="10dp"
    tools:context="org.liangxiaokou.module.album.AlbumActivity">

    <ImageView
        android:focusable="false"
        android:id="@+id/image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/marquee_add_photo_48" />

</LinearLayout>

 

共有 人打赏支持
粉丝 52
博文 335
码字总数 116759
×
executor
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: