Android实例 表白墙

前言:毕业设计没有需求,自己想需求,没什么太多想法,突然想起来有个渤大表白墙这个功能也不错,随便写一下。

具体就是一个弹窗插入数据,获取数据,在ListView中显示适配,下拉刷新。

public class ConfessionFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.msg_love_list)
    ListView msgListView;
    @BindView(R.id.refresh_love)
    SwipeRefreshLayout refreshLove;
    List<MsgLove> mMsgLoveList = new ArrayList<>();
    MsgLoveAdapter mMsgLoveAdapter;

    public static Fragment getInstance() {
        Fragment fragment = new ConfessionFragment();
        return fragment;
    }


    @Override
    protected int contentViewID() {
        return R.layout.fragment_confession;
    }

    @Override
    protected void initialize() {
        EventBus.getDefault().register(this);
        refreshLove.setOnRefreshListener(this);
        refreshLove.setColorSchemeColors(getResources().getColor(R.color.color_love));
        queryLoveMsg();

    }

    private void queryLoveMsg() {
        BmobQuery<MsgLove> msgLoveBmobQuery = new BmobQuery<MsgLove>();
        msgLoveBmobQuery.order("-createdAt");
        msgLoveBmobQuery.findObjects(new FindListener<MsgLove>() {
            @Override
            public void done(List<MsgLove> object, BmobException e) {
                if (e == null) {
                    mMsgLoveList = object;
                    mMsgLoveAdapter = new MsgLoveAdapter(getContext(), R.layout.item_confession, mMsgLoveList);
                    msgListView.setAdapter(mMsgLoveAdapter);
                } else {
                    ToastUtils.showShort(getContext(), getContext().getString(R.string.query_failure));
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        queryLoveMsg();
        mMsgLoveAdapter.notifyDataSetChanged();
        refreshLove.setRefreshing(false);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent messageEvent) {
        switch (messageEvent.getMessage()) {
            case ConstantConfig.UPDATE_LOVE:
                onRefresh();
                break;
            default:
        }
    }


    @OnClick({R.id.btn_add_love})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_add_love:
                DialogUtils.addLoveDialog(getContext());
                break;
            default:
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
}
public class MsgLoveAdapter extends ArrayAdapter<MsgLove> {
    public static final String TAG = "MsgLoveAdapter";
    private int resourceId;

    public MsgLoveAdapter(@NonNull Context context, int resource, @NonNull List<MsgLove> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        MsgLove msgLove = getItem(position);
        ViewHolder viewHolder;
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.loveTo = view.findViewById(R.id.love_to);
            viewHolder.loveFrom = view.findViewById(R.id.love_from);
            viewHolder.loveContent = view.findViewById(R.id.love_content);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.loveTo.setText(msgLove.getTo());
        viewHolder.loveContent.setText(msgLove.getContent());
        viewHolder.loveFrom.setText(msgLove.getFrom());
        return view;

    }

    class ViewHolder {
        TextView loveTo;
        TextView loveFrom;
        TextView loveContent;
        SimpleDraweeView loveImg;
    }
}
public class DialogUtils {
    public static void addLoveDialog(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.add_love_tip, null);
        EditText etLoveTo = view.findViewById(R.id.et_love_to);
        EditText etLoveFrom = view.findViewById(R.id.et_love_from);
        EditText etLoveSays = view.findViewById(R.id.et_love_says);
        Button btnLovePublish = view.findViewById(R.id.btn_love_publish);
        Button btnLoveCancel = view.findViewById(R.id.btn_love_cancel);
        final Dialog dialog = new AlertDialog.Builder(context)
                .setView(view)
                .setCancelable(true)
                .create();
        btnLovePublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(etLoveTo.getText().toString()) || TextUtils.isEmpty(etLoveFrom.getText().toString())
                || TextUtils.isEmpty(etLoveSays.getText().toString())){
                    ToastUtils.showShort(context,context.getString(R.string.tip_empty));
                    return;
                }
                MsgLove msgLove = new MsgLove();
                MyUser myUser = MyUser.getCurrentUser(MyUser.class);
                msgLove.setUsername(myUser.getUsername());
                msgLove.setTo(etLoveTo.getText().toString());
                msgLove.setFrom(etLoveFrom.getText().toString());
                msgLove.setContent(etLoveSays.getText().toString());
                msgLove.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        if (e == null) {
                            ToastUtils.showShort(context,context.getString(R.string.love_send));
                            EventBus.getDefault().post(new MessageEvent(ConstantConfig.UPDATE_LOVE));
                            dialog.dismiss();
                        } else {
                            ToastUtils.showShort(context,context.getString(R.string.love_fail));
                        }
                    }
                });
            }
        });

        btnLoveCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@mipmap/bg_confession"
    android:padding="8dip"
    tools:context=".fragment.ConfessionFragment">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/refresh_love"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/msg_love_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/btn_add_love"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:src="@android:drawable/ic_menu_edit"
        app:backgroundTint="@color/color_love"
        app:borderWidth="0.0dip"
        app:fabCustomSize="40dp" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:orientation="horizontal">

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/love_img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        fresco:roundAsCircle="true"
        android:layout_margin="5dp"
        fresco:placeholderImage="@mipmap/img_confession"
        />

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardCornerRadius="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/love_to"
                    android:textSize="20sp"
                    android:layout_marginRight="5dp"
                    android:textColor="@color/color_love"
                    android:textStyle="bold" />
                <TextView
                    android:id="@+id/love_to"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:textColor="@color/color_love"
                    android:text="@string/app_name"/>
            </LinearLayout>
            <TextView
                android:id="@+id/love_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:text="@string/school_motto"
                android:textSize="15sp"
                android:layout_marginRight="5dp" />

            <TextView
                android:id="@+id/love_from"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:text="@string/anonymous"
                android:layout_gravity="end"
                android:textSize="15sp"
                android:layout_marginRight="5dp" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/love_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/love_add_confession"
            android:textColor="@color/color_love"
            android:textSize="18sp"
            android:textStyle="bold" />

        <LinearLayout
            android:id="@+id/ll_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/love_title"
            android:layout_margin="5dp"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:textSize="15sp"
                    android:text="@string/love_to"/>
                <EditText
                    android:id="@+id/et_love_to"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:hint="@string/love_to_text"
                    />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:textSize="15sp"
                    android:text="@string/love_from"/>
                <EditText
                    android:id="@+id/et_love_from"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/anonymous"
                    />
            </LinearLayout>

            <EditText
                android:id="@+id/et_love_says"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top"
                android:hint="@string/love_say_text"
                android:minLines="10"
                android:textSize="15sp"
                />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ll_content"

            android:orientation="horizontal">

            <Button
                android:id="@+id/btn_love_publish"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_marginLeft="9dp"
                android:layout_marginTop="5dp"
                android:layout_marginRight="20dp"

                android:layout_weight="1"
                android:text="@string/publish"
                android:background="@drawable/selector_btn_love"
                android:textColor="@color/color_ffffff" />


            <Button
                android:id="@+id/btn_love_cancel"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:layout_marginRight="9dp"
                android:layout_weight="1"
                android:text="@string/cancel"
                android:background="@drawable/selector_btn_love"
                android:textColor="@color/color_ffffff"
                android:textSize="15sp" />

        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

 

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南:-)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值