1. 程式人生 > >關於使用PulltoRefreshListView來展示新聞的內容以及多級評論

關於使用PulltoRefreshListView來展示新聞的內容以及多級評論

在專案當中,碰到了使用PulltoRefreshListView來展示新聞和多級評論,在評論那裡碰到了難題,如何解決多級評論的顯示呢?

為了方面起見,我直接使用ListView來代替PulltoRefreshListView做示例。

廢話不多說,直接看程式碼示例。

第一步:首先,新建MainActivity的XML佈局,佈局就一個ListView。之後再新建MainActivity類

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout>
第二步:新建HeaderView的佈局,就是ListView的頭部分,為方便理解,直接使用簡單化來展示 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

    <TextView
android:layout_width="match_parent"
android:layout_height=
"wrap_content" android:text="文章Title" android:textColor="@android:color/holo_red_dark"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textColor="@android:color/holo_green_dark" android:text="文章內容jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaffffffffffffffffffffffffffffffffffffffffffffffrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrreeeeeeeeeeee"/> </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="match_parent"
android:background="@android:color/white"
android:orientation="vertical">

    <TextView
android:id="@+id/tvHotComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:text="熱門評論"
android:textColor="@android:color/holo_red_dark"
android:textSize="20sp" />

    <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="一級評論內容"
android:textColor="@android:color/black" />

    <LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@android:color/darker_gray">

    </LinearLayout>

</LinearLayout>

id為ll的LinearLayout是為了展示二級評論內容的;
第四步:
新建評論介面卡,繼承BaseAdapter,因為是本地展示,所以資料來源直接就以for迴圈來代替了。為了有熱門評論和最新評論之分,在介面卡中,寫入判斷position的位置決定title的顯示與文字的變化。介面卡程式碼如下:
package com.listviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CommentAdapter extends BaseAdapter {
    private Context mContext;

    public CommentAdapter(Context context) {
        mContext=context;
    }

    @Override
public int getCount() {
        return 20;
    }

    @Override
public Object getItem(int i) {
        return null;
    }

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

    @Override
public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.comment_item, null);
            holder=new ViewHolder();
            holder.ll=(LinearLayout) view.findViewById(R.id.ll);
            holder.tvHot=(TextView) view.findViewById(R.id.tvHotComment);
            view.setTag(holder);
        } else {
            holder=(ViewHolder)view.getTag();
        }
        if(position==0){
            holder.tvHot.setVisibility(View.VISIBLE);
            holder.tvHot.setText("熱門評論");
        }else{
            if(position==3){
                holder.tvHot.setVisibility(View.VISIBLE);
                holder.tvHot.setText("最新評論");
            }else {
                holder.tvHot.setVisibility(View.GONE);
            }
        }
        holder.ll.removeAllViews();
        for(int i=0;i<3;++i){
            View commentItem2=LayoutInflater.from(mContext).inflate(R.layout.comment_2_item, null);
            holder.ll.addView(commentItem2);
        }
        return view;
    }

    class ViewHolder{
        LinearLayout ll;
        TextView tvHot;
    }
}
第五步:在介面卡寫好後,在MainActivity方法中對ListView進行處理,使用ListView的特性,addHeaderView(View view)方法,再將介面卡設定進去就完成了。
public class MainActivity extends AppCompatActivity {

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

        ListView listView=(ListView) findViewById(R.id.listView);
        View topView= LayoutInflater.from(this).inflate(R.layout.top_view,null);
        listView.addHeaderView(topView);
        listView.setAdapter(new CommentAdapter(this));
    }

}

效果圖如下:

相關推薦

關於使用PulltoRefreshListView展示新聞的內容以及多級評論

在專案當中,碰到了使用PulltoRefreshListView來展示新聞和多級評論,在評論那裡碰到了難題,如何解決多級評論的顯示呢? 為了方面起見,我直接使用ListView來代替PulltoRefreshListView做示例。 廢話不多說,直接看程式碼示例。 第一步:

Android項目實戰(十六):QQ空間實現(一)—— 展示說說中的評論內容並有相應點擊事件

con toast short demo append 集合 obj parent 自帶 原文:Android項目實戰(十六):QQ空間實現(一)—— 展示說說中的評論內容並有相應點擊事件大家都玩QQ空間客戶端,對於每一個說說,我們都可以評論,那麽,對於某一條評論:

R+tmcn筆記︱tmcn包的基本內容以及李艦老師R語言大會展示內容摘錄

每每以為攀得眾山小,可、每每又切實來到起點,大牛們,緩緩腳步來俺筆記葩分享一下吧,please~———————————————————————————install.packages("tmcn", r

用隊列處理訂單以及集合間性能比較

ado 而是 div don 指定 bre 移動 edev err 數組的大小是固定的,如果元素個數是動態的,就應使用集合類,集合可以根據集合類實現的接口組合為列表、集合和字典,集合實現的常用接口有如下 (1)IEnumerable<T>:如果將foreach

day20 project+查看新聞列表 + 點贊 + 圖片驗證碼 + 評論多級評論 + 後臺管理 + webSocket + kindEditor

dump 用戶 opacity message itl render dmi 其他 bsp Day20回顧: 1. 請求生命周期 2. 中間件   md = [     "file_path.classname"

報障系統之多級評論前戲

true none _id bsp nbsp 多級 append clas tco 前戲: 列表添加: v1 = [1,2,3,4] v1.append(123) print(v1) data = [ [11,22,33],

Django多級評論

new {} highlight child 發現 有變 數據 基礎 end 一、原理 #多級評論原理簡單原理,弄完之後發現基礎白學了 msg_list = [ {‘id‘:1,‘content‘:‘xxx‘,‘parent_id‘:None}, {‘id

多級評論

再次 bsp 無需 ash div default 需要 想要 數據 # data=[ # [11,22,33], # [44,55,66] # ] # data[0].append(data[1]) # print(data) # data[1].app

EasyUI Tree 樹 ——實現多級別菜單的展示以及與後臺數據的交互

container node 頁面加載 sco view 調用 .... csharp cat 一 要引入的js css庫 <link type="text/css" href="css/base.css" rel="stylesheet" />

編程語言和shell編程的基礎內容以及grep、egrep命令及相應的正則表達式和用法

編程語言和shell編程的基礎內容以及grep、egrep命令及相應的正則表達式和用法bash的特性之多命令執行的邏輯關系: 1.命令替換(命令之間無明確的邏輯依賴關系) COMMAND1 $(COMMAND2) 2.管道(命令之間無明確的邏輯依賴關系) COMMAND1 | COMMAND2

通過FTP服務端實現匿名用戶和基本用戶的訪問,可以實現文件上傳和下載。通過web網站瀏覽內容

ges 用戶 分享圖片 size 下載 mark col term http 通過FTP服務端來實現匿名用戶和基本用戶的訪問,可以實現文件上傳和下載。通過web網站來瀏覽內容

多級評論的實現

models none 如果 lse print 通過 ldr model 重點 comment_list=models.Comment.objects.filter(news_id=new_id) ret=[] # 最終拿到的數據 comment_list_dict={

數據處理為樹形結構以及多級菜單的邏輯分析

value data 關系 type 二級 相同 item 樹形結構 調用 菜單數據源處理: //數據源組裝成樹形結構(一級與後面的級邏輯相同)邏輯:遍歷所有數據,上級與下次的聯動關系相等則數據存放,如果不為最後一級,遞歸function getTree(source, d

vgg的grad作為激活值展示圖片物體

vision span 什麽 normal size 激活 ali nbsp .com import torch import numpy import torch.nn as nn import torch.nn.functional as F from PIL imp

Java開發中,通過sql實現過濾以及分頁

  我們通過一個需求來引入問題。 首先,簡單介紹一下需求:實現一個下圖的頁面,包含了過濾以及分頁。資料是後臺資料庫獲得到的。 那麼關於如何實現過濾以及分頁,考慮到通過前臺實現或者後臺實現,我們不妨來分析一下兩種方法。 首先,前臺實現:無非就是後臺獲取所有的list,傳

利用scrapy獲取抽屜新熱榜的標題和內容以及新聞地址儲存到本地

1、安裝scrapy   pip3 install scrapy 2、開啟terminal,cd 到想要建立程式的目錄下 3、建立一個scrapy專案   在終端輸入:scrapy startproject my_first_scrapy(專案名) 4、在終端輸入:cd my_first_

如何在github上fork一個專案貢獻程式碼以及同步原作者的修改

作為一個IT人,通過github進行學習是最快的成長手段。我們可以瀏覽別人的優秀程式碼。但只看不動手還是成長得很慢,因此為別人貢獻程式碼才是明智之舉。比如我們看下片看,許多大片都是由字幕組免費翻譯壓制的。為什麼他們要這樣做呢?因為他們都是聰明的大學生,為了提高聽力水平,提高筆譯水平才這樣乾的!中國人

通過透明Activity展示新手指引蒙層

上一篇文章介紹了通過Dialog的方式來處理蒙層詳情,但是使用的時候還是有侷限性,由於dialog是要依附於某個具體的Activity才能彈出顯示的,當依附的Activity finish後Dialog也會跟著消失,對於一些特定的需求當依附的Activity消失後仍需要顯示蒙層,只有使用者點選了

通過Dialog展示新手指引蒙層

以前展示新手指引蒙層的方式都是在Activity基類的根佈局中疊加上一個全屏的ImageView,然後在條件成立的時候設定蒙層並顯示,這裡介紹一種通過Dialog來展示的方式,用起來比較靈活. 先看dialog的佈局,其實就是一個ImageView <?xml version="1

【PHP常見面試題 程式功能設計】先寫一個線上留言本,實現使用者的線上留言功能,留言資訊儲存到資料庫,要求書籍資料表內容以及使用PHP編碼完成。

一、考點 1、資料表設計 分析資料表結構 留言板有哪些資訊需要儲存? 留言資訊:ID,留言標題,留言內容,留言時間,留言人 2、資料表建立語句 // 留言本表 message create table message( id int unsign