1. 程式人生 > 程式設計 >Android 自定義ListView實現QQ空間介面(說說內包含圖片、視訊、點贊、評論、轉發功能)

Android 自定義ListView實現QQ空間介面(說說內包含圖片、視訊、點贊、評論、轉發功能)

前端時間剛好需要做一個類似於QQ空間的社群分享功能,說說內容包含文字(話題、內容)、視訊、圖片,還需包含點贊,評論,位置資訊等功能。 就採用LIstview做了一個,先來看下效果,GIF太大,CSDN傳不了,請移步Gitee連線:GIF效果

在這裡插入圖片描述

1. 先來分析一下ListView中每一個條目包含的控制元件,請看下圖

在這裡插入圖片描述

序號1:頭像,ImageView,自定義為圓形即可;
序號2:使用者名稱,TextView;
序號3:釋出時間,TextView;
序號4:說說文字部分,TextView;
序號5:說說中視訊或圖片部分,Videoview;
序號6:點贊資訊,TextView,動態新增;
序號7:位置資訊,TextView;

序號8/9/10:點贊、評論、轉發,均為ImageView;
序號11:評論區,TextView,動態新增;
序號12:評論框,EditText,其右側圖片是通過drawableRight設定的,事件監聽會在後面詳細說;

上面圖中漏了一個,在視訊正中央還需要有一個播放按鈕,為ImageView,通過切換ImageView中圖片實現播放與暫停切換。

2. 確定好有哪些控制元件後,我們用xml實現佈局,檔案命名為video_brower_item.xml,程式碼如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:app="http://schemas.android.com/apk/res-auto">
 <LinearLayout
  android:id="@+id/mContainer"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:paddingLeft="10dp"
  android:paddingRight="10dp"
  android:paddingTop="10dp"
  android:background="@android:color/white">
  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
   <com.xiaok.winterolympic.custom.CircleImageView
    android:id="@+id/video_avatar"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:src="@drawable/head_picture" />
   <TextView
    android:id="@+id/video_username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="xiaok"
    android:textColor="#000000"
    android:layout_marginStart="15dp"
    android:textSize="24sp"
    android:textStyle="bold" />
   <TextView
    android:id="@+id/video_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:textSize="14sp"
    android:text="剛剛"/>
  </LinearLayout>
  <TextView
   android:id="@+id/video_descripation"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginTop="15dp"
   android:textSize="16sp"
   android:textColor="#000000"
   android:text="#共迎冬奧# 冬奧"/>
  <VideoView
   android:id="@+id/video_view"
   android:layout_width="match_parent"
   android:layout_height="230dp"
   android:layout_marginTop="15dp"/>
  <RelativeLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
   <TextView
    android:id="@+id/video_position"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="北京市朝陽區"
    android:layout_marginTop="12dp"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="10dp"/>
   <ImageView
    android:id="@+id/video_iv_good"
    style="@style/VideoShareImageView"
    android:src="@mipmap/video_share_good"
    android:layout_toStartOf="@+id/video_iv_comment"
    android:layout_marginEnd="20dp"/>
   <ImageView
    android:id="@+id/video_iv_comment"
    style="@style/VideoShareImageView"
    android:src="@mipmap/video_share_comment"
    android:layout_toStartOf="@+id/video_iv_share"
    android:layout_marginEnd="20dp"/>
   <ImageView
    android:id="@+id/video_iv_share"
    style="@style/VideoShareImageView"
    android:src="@mipmap/video_share_share"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="10dp"/>
  </RelativeLayout>
  <EditText
   android:id="@+id/video_et_comment"
   android:layout_width="match_parent"
   android:layout_height="40dp"
   android:hint="評論"
   android:textSize="14sp"
   android:layout_marginBottom="20dp"
   android:drawableRight="@drawable/video_send_picture"/>
 </LinearLayout>
 <ImageView
  android:id="@+id/video_play"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@mipmap/ic_record_play"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="192dp"/>
</FrameLayout>

3. 定義一個類,這裡命名為VideoBrower,用於封裝ListView中每個條目所用到的資料:

package com.xiaok.winterolympic.model;
import java.io.Serializable;
public class VideoBrower implements Serializable {
  private static final long serialVersionUID = 1L;
  private int avatarId;
  private String username;
  private String date;
  private String videoDescripation;
  private String videoPath;
  private String position;
  public VideoBrower(int avatarId,String username,String date,String videoDescripation,String videoPath,String position) {
    this.avatarId = avatarId;
    this.username = username;
    this.date = date;
    this.videoDescripation = videoDescripation;
    this.videoPath = videoPath;
    this.position = position;
  }
  public int getAvatarId() {
    return avatarId;
  }
  public String getUsername() {
    return username;
  }
  public String getDate() {
    return date;
  }
  public String getVideoDescripation() {
    return videoDescripation;
  }
  public String getVideoPath() {
    return videoPath;
  }
  public String getPosition() {
    return position;
  }
  public void setAvatarId(int avatarId) {
    this.avatarId = avatarId;
  }
  public void setDate(String date) {
    this.date = date;
  }
  public void setUsername(String username) {
    this.username = username;
  }
  public void setVideoDescripation(String videoDescripation) {
    this.videoDescripation = videoDescripation;
  }
  public void setVideoPath(String videoPath) {
    this.videoPath = videoPath;
  }
  public void setPosition(String position) {
    this.position = position;
  }
}

這裡解釋下,頭像我是通過封裝R檔案中對應的資源ID實現的,所以格式為int,其他應該不用解釋。

總結

以上所述是小編給大家介紹的Android 自定義ListView實現QQ空間介面,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!