Android——“i 分享”APP开发Day09

在上一篇已经搭建好详情页的后台,接下来主要完成Android页面的功能逻辑,内容包括以下几个部分

  • 根据不同的内容类型显示不同的页面结构——分享图片类型的内容详情带有图片,其他类型的内容隐藏图片显示框
  • 详情页包括的内容有:标题、作者、简述、详情、点赞数
  • 进入该页面时判断文章是否属于用户本人,是的话就显示删除按钮,并可以进行删除文章的操作
  • 进入详情页之后还需要判断用户是否已经收藏该内容,收藏的情况下收藏图标显示收藏,并可以进行取消收藏的操作
  • 评论内容的输入与发布
  • 评论内容展示列表

1.因为需要有评论列表,所以在设计内容详情页面之前要在layout下先新建评论item的展示文件——activity_comment_list.xml

  • 评论内容包括用户昵称和评论的详细内容,具体代码如下
<?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:padding="20dp"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <ImageView
            android:id="@+id/cardImgTip"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:src="@drawable/comment_head"
            />
        <TextView
            android:id="@+id/comment_user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:paddingLeft="30dp"
            android:textSize="18sp"
            android:textStyle="bold"
            android:textColor="#513861"
            android:text="评论者昵称" />
        <TextView
            android:id="@+id/comment_id"
            android:visibility="gone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:textSize="18sp"
            android:textColor="#524A47"
            android:text="评论的ID" />
    </RelativeLayout>

    <TextView
        android:id="@+id/comment_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#000000"
        android:text="这里放的是评论内容"/>
<!--
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="#000000"/>-->

</LinearLayout>

2. 设计内容的详情页面,在layout下xi——activity_info_detail.xml

  • 顶部是图片展示框(默认隐藏),依次是标题、作者、内容简述、内容详情、点赞收藏
  • 详细代码如下 :
<?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:orientation="vertical">

    <FrameLayout
        android:id="@+id/share_detail_img"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/share_img_2">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="35dip"
            android:layout_gravity="bottom"
            android:background="#73000000"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/share_img_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="今日分享:分享每一处美景"
                android:textColor="@android:color/white" />
        </LinearLayout>
    </FrameLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fadingEdge="none" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:paddingLeft="14dp"
        android:paddingRight="14dp"
        android:paddingBottom="60dp"
        android:orientation="vertical">
        <TextView
            android:id="@+id/findDetail_info_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22dp"
            android:textStyle="bold"
            android:text="这是文章的标题,不长" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">
            <TextView
                android:id="@+id/findDetail_info_author"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:textSize="18sp"
                android:layout_weight="1"
                android:textColor="#000000"
                android:text="作者昵称" />

            <ImageView
                android:id="@+id/findDetail_author_like"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitEnd"
                android:src="@drawable/add_focus"
                />
        </LinearLayout>

        <TextView
            android:background="@drawable/list_card_style"
            android:id="@+id/findDetail_info_describe"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_10"
            android:padding="10dp"
            android:textSize="18sp"
            android:textColor="#000000"
            android:text="这里放一些内容,主要是对下面详细内容的简述,具体根据文章类型变化"/>
        <TextView
            android:id="@+id/detail_info_detail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_10"
            android:padding="10dp"
            android:textSize="18sp"
            android:textColor="#000000"
            android:singleLine="false"
            android:text="这里放一些文章的详细内容,具体会根据文章类型变化
这里放一些文章的详细内容,具体会根据文章类型变化这里放一些文章的详细内容,具体会根据文章类型变化\n\n
这里放一些文章的详细内容,具体会根据文章类型变化
这里放一些文章的详细内容,具体会根据文章类型变化这里放一些文章的详细内容,具体会根据文章类型变化
"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">
            <!--
            <TextView
                android:id="@+id/findDetail_info_support"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:textSize="18sp"
                android:layout_weight="1"
                android:textColor="#000000"
                android:text="点赞数" />
            <ImageView
                android:id="@+id/findDetail_support_img"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitEnd"
                android:src="@drawable/my_tab_list2"
                />-->

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="1.0dip"
                android:shrinkColumns="0"
                android:stretchColumns="0" >

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2.0dip"
                    android:layout_marginRight="2.0dip">

                    <TextView
                        android:id="@+id/detail_info_support"
                        android:layout_width="wrap_content"
                        android:layout_marginRight="200dp"
                        android:layout_height="fill_parent"
                        android:drawableLeft="@drawable/not_support"
                        android:gravity="center_vertical"
                        android:includeFontPadding="false"
                        android:paddingLeft="4.0dip"
                        android:text="点赞数"
                        android:textColor="#ff333333"
                        android:textSize="16.0sp" />
                    <ImageButton
                        android:id="@+id/detail_info_delete"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:layout_gravity="right"
                        android:gravity="center_vertical"
                        android:paddingRight="20.0dip"
                        android:background="#00000000"
                        android:visibility="gone"
                        android:src="@drawable/delete_info" />
                    <ImageButton
                        android:id="@+id/detail_info_focus"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:layout_gravity="right"
                        android:gravity="center_vertical"
                        android:paddingRight="10.0dip"
                        android:background="#00000000"
                        android:src="@drawable/now_not_like" />
                </TableRow>
            </TableLayout>



        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/small_bc">

            <!-- 输入的评论信息 -->
            <EditText
                android:id="@+id/et_comment_detail"
                android:paddingLeft="20dp"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_weight="4"
                android:background="@null"
                android:gravity="center_vertical"
                android:hint="输入评论"
                android:textColor="#0e0e0e"
                android:textColorHint="#b0c6ce"
                android:textSize="17sp" />

            <!--  评论 -->
            <Button
                android:id="@+id/comment_btn"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_gravity="right|center_vertical"
                android:layout_margin="24dp"
                android:background="@drawable/add_comment" />
        </LinearLayout>

        <LinearLayout
            android:layout_below="@+id/title_bar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical"
            android:paddingTop="10dp">
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:background="#000000"/>

            <ListView
                android:id="@+id/comment_listView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:dividerHeight="0dp" />
        </LinearLayout>
    </LinearLayout>
    </ScrollView>
</LinearLayout>

3. 接下来开始处理页面逻辑,再之前建好的包com.example.discover中新建class命名为DisplayDetail

  • 因为是跳转页面,要在AndroidMenifest.xml中添加页面配置
    <activity android:name=".DisplayDetail"
        android:label="查看详情">
    </activity>
  • 获取到上一个页面传的参数infoId,用该参数去请求之前写好的servlet——QueryInfoById,取得对应内容的所有信息并判断内容类型,根据类型控制图片显示框的显示状态
  • 取得用户的昵称和内容Id之后请求之前写好的servlet——IfAddFocus,得到用户是否已经收藏该内容,如果收藏则将收藏按钮状态设置为已收藏,反之设置为未收藏状态
  • 取得用户昵称与返回的作者信息对比,如果相同则显示删除按钮,并给删除按钮添加删除事件——请求DeleteInfo
  • 取得内容Id之后请求之前写好的servlet——QueryComment,得到内容的评论列表
  • 取得用户的昵称和内容的ID,在用户评论的时候请求之前写好的servlet——AddComment,增加评论,在评论成功之后清空输入框的内容并刷新适配器

——DisplayDetail.java的详细代码如下

package com.example.discover;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bean.CommentInfo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class DisplayDetail extends Activity{
    String baseUrl = "http://10.0.2.2:8080/iShareService/servlet/";   //web服务器的地址
    String imgBaseUrl = "http://10.0.2.2:8080/iShareService/images/";  //图片资源

    TextView tv_info_author; //内容作者
    TextView tv_info_title; //内容标题
    TextView tv_info_describe; //内容简述
    TextView tv_info_detail; //内容详情
    TextView tv_info_support; //内容点赞数
    ImageButton btn_info_like;   //内容收藏
    ImageButton btn_info_delete;   //内容删除

    EditText et_comment_detail;  //评论的内容
    Button comment_btn;   //评论按钮

    TextView share_img_title; //图片背景上的文字
    FrameLayout share_detail_img;   //分享的图片背景

    String infoId;
    String infoTitle;   //内容标题
    String infoDescribe;   //内容简述
    String infoDetail;   //内容详情
    Integer type;    //类型:0表示日记,1表示趣事
    Integer support;   //点赞数
    String infoAuthor;  //作者

    Boolean isFocus = false;  //是否收藏
    int focusId;  //收藏ID

    Boolean isMyInfo = false;
    private DisplayDetail.CommentAdapter commentAdapter;   //存放评论
    private int dataSize = 28;     //数据集的条数
    private ListView listView;


    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_detail);

        tv_info_author = (TextView)findViewById(R.id.findDetail_info_author);  //文章作者

        tv_info_title = (TextView)findViewById(R.id.findDetail_info_title);  //文章标题
        tv_info_describe = (TextView)findViewById(R.id.findDetail_info_describe);  //文章简述
        tv_info_detail = (TextView)findViewById(R.id.detail_info_detail);  //文章详细内容
        tv_info_support  = (TextView)findViewById(R.id.detail_info_support);  //文章点赞数
        btn_info_like = (ImageButton)findViewById(R.id.detail_info_focus);   //文章收藏
        btn_info_delete = (ImageButton)findViewById(R.id.detail_info_delete);   //文章收藏

        et_comment_detail = (EditText) findViewById(R.id.et_comment_detail);  //评论的内容
        comment_btn = (Button)findViewById(R.id.comment_btn);   //评论按钮

        share_img_title = (TextView)findViewById(R.id.share_img_title);  //图片背景的文字
        share_detail_img  = (FrameLayout)findViewById(R.id.share_detail_img);  //图片背景的文字

        //取得启动该Activity的Intent对象
        Intent intent =getIntent();
        //取出Intent中附加的数据
        if(intent.getStringExtra("infoId")!=null){
            infoId =intent.getStringExtra("infoId");
            initialize(infoId);   //设置页面内容
            initializeFocus(infoId);   //改变收藏状态

            tv_info_support.setOnClickListener(new supportClick());
            btn_info_like.setOnClickListener(new focusClick());
            btn_info_delete.setOnClickListener(new deleteClick());
            listView = (ListView)findViewById(R.id.comment_listView);
            initializeAdapter();   //初始化,填入评论
            listView.setAdapter(commentAdapter);
            setListViewHeightBasedOnChildren(listView);
            comment_btn.setOnClickListener(new commentClick());
        }

    }


    /**
     * 查询数据库中的数据
     */
    private JSONArray loadData(String QueryInfoUrl){
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader buffer = null;
        HttpURLConnection connGET = null;

        try {
            URL url = new URL(QueryInfoUrl);
            connGET = (HttpURLConnection) url.openConnection();
            connGET.setConnectTimeout(5000);
            connGET.setRequestMethod("GET");
            if (connGET.getResponseCode() == 200) {
                buffer = new BufferedReader(new InputStreamReader(connGET.getInputStream()));
                for (String s = buffer.readLine(); s != null; s = buffer.readLine()) {
                    stringBuilder.append(s);
                }
                //返回测试信息
                JSONArray jsonArray = new JSONArray(stringBuilder.toString());

                //获取到的数据,对Json进行解析
                buffer.close();
                return jsonArray;
            }else{
                Toast.makeText(DisplayDetail.this,"非200", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(DisplayDetail.this, "get 提交 err.." + e.toString(), Toast.LENGTH_LONG).show();
        }
        return null;
    }

    /**
     * 初始化ListView的适配器,即打开页面展示的评论
     */
    private void initializeAdapter(){
        String QueryCommentUrl = baseUrl+"QueryComment?infoId="+infoId;
        JSONArray jsonArray = loadData(QueryCommentUrl);

        if(jsonArray!=null) {
            try {
                JSONObject totalObject = (JSONObject) jsonArray.get(0);

                dataSize = totalObject.getInt("totalRecord");  //总记录数
                String detail = totalObject.getString("RecordDetail");   //详情

                if (initSetDataToBean(detail) != null) {
                    commentAdapter = new DisplayDetail.CommentAdapter(initSetDataToBean(detail));   //将详情设置到bean中
                }

            } catch (JSONException e) {
                Toast.makeText(DisplayDetail.this, "initializeAdapter异常 err.." + e.toString(), Toast.LENGTH_LONG).show();
            }
        }else{
            Toast.makeText(DisplayDetail.this, "查询评论为空" , Toast.LENGTH_LONG).show();
        }

    }


    /*刷新适配器*/
    public  void refreshDiscover(){
        listView.setAdapter(null);   //将ListView控件的内容清空
        initializeAdapter();
        listView.setAdapter(commentAdapter);
        commentAdapter.notifyDataSetChanged();
        setListViewHeightBasedOnChildren(listView);
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

         int totalHeight = 0;
         for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
                // listAdapter.getCount()返回数据项的数目
                 View listItem = listAdapter.getView(i, null, listView);
                 // 计算子项View 的宽高
                 listItem.measure(0, 0);
              // 统计所有子项的总高度
                totalHeight += listItem.getMeasuredHeight();
             }

         ViewGroup.LayoutParams params = listView.getLayoutParams();
         params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
       // listView.getDividerHeight()获取子项间分隔符占用的高度
       // params.height最后得到整个ListView完整显示需要的高度
         listView.setLayoutParams(params);
    }

    //评论
    class commentClick implements View.OnClickListener {
        public void onClick(View v) {
            SharedPreferences sp = DisplayDetail.this.getSharedPreferences("save", Context.MODE_PRIVATE);
            String username = sp.getString("name","");

            String commentDetail = et_comment_detail.getText().toString();

            String AddSupportUrl = baseUrl+"AddComment?infoId="+infoId+"&commentUser="+username+"&commentDetail="+commentDetail;
            JSONArray jsonArray = loadData(AddSupportUrl);
            try {

                JSONObject totalObject = (JSONObject)jsonArray.get(0);
                Boolean addOk = totalObject.getBoolean("addOk");  //是否评论成功

                if(addOk){
                    refreshDiscover(); //刷新适配器
                    et_comment_detail.setText("");
                }else{
                    Toast.makeText(DisplayDetail.this, "评论失败,请稍后再试" , Toast.LENGTH_LONG).show();
                }

            }catch (JSONException e){
                Toast.makeText(DisplayDetail.this, "initialize异常 err.." + e.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }

    //点赞
    class supportClick implements View.OnClickListener {
        public void onClick(View v) {
            support = support+1;
            String AddSupportUrl = baseUrl+"AddSupport?InfoId="+infoId;
            JSONArray jsonArray = loadData(AddSupportUrl);
            try {

                JSONObject totalObject = (JSONObject)jsonArray.get(0);
                Boolean addOk = totalObject.getBoolean("addOk");  //是否点赞成功

                if(addOk){
                    overSupport();  //改变点赞图片状态为已经点赞
                }else{
                    support = support-1;  //没有点赞成功
                }

            }catch (JSONException e){
                Toast.makeText(DisplayDetail.this, "initialize异常 err.." + e.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }

    //启动线程,加载主页
    private class LoadMainTask implements Runnable{
        @Override
        public void run() {
            Intent intent = new Intent(DisplayDetail.this,MainActivity.class);
            startActivity(intent);  //打开主页
            finish();   //关闭
        }
    }

    //删除文章
    class deleteClick implements View.OnClickListener {
        public void onClick(View v) {
            String deleteInfoUrl = baseUrl+"DeleteInfo?infoId="+infoId;
            JSONArray jsonArray = loadData(deleteInfoUrl);
            try {

                JSONObject totalObject = (JSONObject)jsonArray.get(0);
                Boolean deleteOk = totalObject.getBoolean("deleteOk");  //是否点赞成功

                if(deleteOk){
                    Toast.makeText(DisplayDetail.this, "删除成功", Toast.LENGTH_LONG).show();
                    //通过handler设置延时1秒后执行r任务,跳转到首页
                    new Handler().postDelayed(new DisplayDetail.LoadMainTask(),1000);

                }else{
                    Toast.makeText(DisplayDetail.this, "删除失败,请稍后再试", Toast.LENGTH_LONG).show();
                }

            }catch (JSONException e){
                Toast.makeText(DisplayDetail.this, "initialize异常 err.." + e.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }


    //收藏或者取消收藏
    class focusClick implements View.OnClickListener {
        public void onClick(View v) {
            if(isFocus){
                //取消收藏
                String removeFocusUrl = baseUrl+"RemoveFocus?focusId="+focusId;
                JSONArray jsonArray = loadData(removeFocusUrl);

                try {
                    JSONObject totalObject = (JSONObject)jsonArray.get(0);
                    Boolean removeOk = totalObject.getBoolean("removeOk");  //是否取消收藏

                    if(removeOk){
                        nowNotLike();  //改变收藏状态为未收藏
                    }else{
                        Toast.makeText(DisplayDetail.this, "取消收藏失败,请稍后再试.", Toast.LENGTH_LONG).show();
                    }
                }catch (JSONException e){
                    Toast.makeText(DisplayDetail.this, "initialize异常 err.." + e.toString(), Toast.LENGTH_LONG).show();
                }

            }else{
                //添加到我的收藏
                SharedPreferences sp = DisplayDetail.this.getSharedPreferences("save", Context.MODE_PRIVATE);
                String username = sp.getString("name","");
                String addFocusUrl = baseUrl+"AddFocus?InfoId="+infoId+"&username="+username;
                JSONArray jsonArray = loadData(addFocusUrl);

                try {
                    JSONObject totalObject = (JSONObject)jsonArray.get(0);
                    Boolean addOk = totalObject.getBoolean("addOk");  //是否收藏成功
                    if(addOk){
                        overFocus();  //改变收藏状态为已经收藏
                        Toast.makeText(DisplayDetail.this, "收藏成功!", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(DisplayDetail.this, "收藏失败,请稍后操作", Toast.LENGTH_LONG).show();
                    }

                }catch (JSONException e){
                    Toast.makeText(DisplayDetail.this, "initialize异常 err.." + e.toString(), Toast.LENGTH_LONG).show();
                }
            }
        }
    }


    /**
     * 初始化,即打开页面展示的数据
     */
    private void initialize(String info_id){
        String QueryInfoUrl = baseUrl+"QueryInfoById?infoId="+info_id;
        JSONArray jsonArray = loadData(QueryInfoUrl);

        try {
            JSONObject totalObject = (JSONObject)jsonArray.get(0);

            infoTitle = totalObject.getString("infoTitle");  //内容标题
          /*  Toast.makeText(DisplayDetail.this,totalObject.toString(), Toast.LENGTH_LONG).show();*/
            infoDescribe = totalObject.getString("infoDescribe");    //内容简述
        /*    infoDetail = totalObject.getString("infoDetail");  //内容详情*/
            String str = totalObject.getString("infoDetail");
            infoDetail =str.replaceAll("<br>","\n");   //获取回车符

            infoAuthor = totalObject.getString("infoAuthor");    //内容作者
            type = totalObject.getInt("infoType");  //内容类型
            support = totalObject.getInt("infoSupport");  //点赞数

            tv_info_title.setText(infoTitle);
            tv_info_author.setText(infoAuthor);
            tv_info_support.setText(support.toString());

            SharedPreferences sp = DisplayDetail.this.getSharedPreferences("save", Context.MODE_PRIVATE);
            String userName = sp.getString("name","");

            if(infoAuthor.equals(userName)){
                displayDelete();  //显示删除按钮
            }else{
                hideDelete(); //隐藏删除按钮
            }

            if(type==0||type==3){
                setArticlesDiaries();
            }else if(type==1){
                //分享图片
                setImg();
            }else if(type==2){
                //分享的音乐
                setArticlesDiaries();
            }
        }catch (JSONException e){
            Toast.makeText(DisplayDetail.this, "initialize异常 err.." + e.toString(), Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 初始化,是否收藏
     */
    private void initializeFocus(String info_id){
        //获取最新的登录状态,SharePreferences为永久存储,需要手动退出
        SharedPreferences sp = DisplayDetail.this.getSharedPreferences("save", Context.MODE_PRIVATE);
        String username = sp.getString("name","");

        String QueryIfFocusUrl = baseUrl+"IfAddFocus?username="+username+"&infoId="+info_id;
        JSONArray jsonArray = loadData(QueryIfFocusUrl);

        try {
            JSONObject totalObject = (JSONObject)jsonArray.get(0);
            isFocus = totalObject.getBoolean("isFocus");  //是否收藏

            //Toast.makeText(DisplayDetail.this, "是否收藏" + isFocus.toString(), Toast.LENGTH_LONG).show();

            if(isFocus){
                overFocus();  //改变收藏图片状态为已经收藏
                focusId = totalObject.getInt("focusId");  //设置收藏Id
            }

        }catch (JSONException e){
            Toast.makeText(DisplayDetail.this, "initialize异常 err.." + e.toString(), Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 改变为已点赞
     */
    public void overSupport(){
        Drawable drawableLeft = getResources().getDrawable(
                R.drawable.over_support);

        tv_info_support.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
                null, null, null);
        tv_info_support.setCompoundDrawablePadding(4);

        tv_info_support.setText(support.toString());  //更新点赞数
    }


    /**
     * 改变为已收藏
     */
    public void overFocus(){
        btn_info_like.setImageResource(getResources().getIdentifier(
                "over_focus", "drawable",
                DisplayDetail.this.getPackageName()));
        isFocus = true;
    }

    /**
     * 改变为未收藏
     */
    public void nowNotLike(){
        btn_info_like.setImageResource(getResources().getIdentifier(
                "now_not_like", "drawable",
                DisplayDetail.this.getPackageName()));
        isFocus = false;
    }
    /**
     * 隐藏文章简述
     */
    public void hideDelete(){
        btn_info_delete.setVisibility(View.GONE);  //隐藏删除
    }

    /**
     * 显示文章简述
     */
    public void displayDelete(){
        btn_info_delete.setVisibility(View.VISIBLE);   //显示删除
    }

    /**
     * 隐藏文章简述
     */
    public void hideDescribe(){
        tv_info_describe.setVisibility(View.GONE);  //隐藏文章简述
    }

    /**
     * 显示文章简述
     */
    public void displayDescribe(){
        tv_info_describe.setVisibility(View.VISIBLE);   //显示文章简述
    }

    /**
     * 隐藏图片
     */
    public void hideImg(){
        share_detail_img.setVisibility(View.GONE);  //隐藏文章简述
    }

    /**
     * 显示图片
     */
    public void displayImg(){
        share_detail_img.setVisibility(View.VISIBLE);  //显示图片
    }

    /**
     * 对于文章和日记类的文章
     */
     public void setArticlesDiaries(){
         tv_info_describe.setText(infoDescribe);
         tv_info_detail.setText(infoDetail);
     }


    /**
     * 对于文章和日记类的文章
     */
    public void setImg(){
        share_img_title.setText(infoTitle);
        tv_info_detail.setText(infoDetail);

        Bitmap one;

        //通过网络链接获取图片
        try {
            one= LoadImgByNet.getBitmap(imgBaseUrl+infoDescribe.trim());
            share_detail_img.setBackground(new BitmapDrawable(getResources(),one));    //以背景的方式设置图片
        }catch(IOException e){
            e.printStackTrace();
        }

        hideDescribe();  //隐藏文章简述
        displayImg();  //显示图片
    }



    //初始化将详情设置到FindInfo bean中
    public List<CommentInfo> initSetDataToBean(String detail){
        List<CommentInfo> CommentInfo = new ArrayList<CommentInfo>();
        try {
            JSONArray detailJsonArray = new JSONArray(detail);
            for (int i = 0; i < detailJsonArray.length(); i++) {
                CommentInfo items = new CommentInfo();

                JSONObject temp = (JSONObject) detailJsonArray.get(i);

                int commentId = temp.getInt("commentId");   //评论的文章ID
                String commentUser = temp.getString("commentUser");    //用户昵称
                int commentInfo = temp.getInt("commentInfo");   //评论的文章ID
                String commentDetail = temp.getString("commentDetail");   //评论详情

                items.setCommentId(commentId);
                items.setInfoId(commentInfo);
                items.setCommentUser(commentUser);
                items.setCommentDetail(commentDetail);
                CommentInfo.add(items);
            }
            return CommentInfo;

        }catch (JSONException e){
            Toast.makeText(DisplayDetail.this, "initSetPeopleDataToBean异常 err.." + e.toString(), Toast.LENGTH_LONG).show();
            return null;
        }
    }

    /**
     * 根据返回的用户设置卡片的样式
     */

    class CommentAdapter extends BaseAdapter {

        List<CommentInfo> newsItems;

        public CommentAdapter(List<CommentInfo> newsitems){
            this.newsItems = newsitems;
        }

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

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

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


        //在这里将Item设置到每个卡片
        @Override
        public View getView(int position, View view, ViewGroup parent) {
            if(view == null){
                view = getLayoutInflater().inflate(R.layout.activity_comment_list, null);
            }

            //用户昵称
            TextView tvTitle = (TextView)view.findViewById(R.id.comment_user_name);
            tvTitle.setText(newsItems.get(position).getCommentUser());

            //用户ID
            TextView getTvId = (TextView)view.findViewById(R.id.comment_id);
            getTvId.setText(newsItems.get(position).getInfoId().toString());

            //评论内容
            TextView tvComment = (TextView)view.findViewById(R.id.comment_detail);
            tvComment.setText(newsItems.get(position).getCommentDetail());

            return view;
        }

        /**
         * 添加数据列表项
         * @param infoItem
         */
        public void addNewsItem(CommentInfo infoItem){
            newsItems.add(infoItem);
        }

    }

}

4.基本上详情页面的功能就完成了,接下来就是要给发现页、查找页的listItem添加点击跳转到详情页的事件

  • 打开之前写好的DiscoverTabFragment.java,给ListView的Item添加点击监听
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView getTvId = (TextView)view.findViewById(R.id.cardId);
        /*Toast.makeText(mActivity,"点击了"+getTvId.getText().toString()+"id", Toast.LENGTH_SHORT).show();*/
        SharedPreferences sp = mActivity.getSharedPreferences("save", Context.MODE_PRIVATE);
        Boolean isLogin = sp.getBoolean("isLogin",false);

        if(isLogin){
            //点击了进入详情
            Intent display_info_intent = new Intent();
            display_info_intent.setClass(mActivity, DisplayDetail.class);  //创建intent对象,并制定跳转页面
            display_info_intent.putExtra("infoId",getTvId.getText().toString());
            mActivity.setResult(1, display_info_intent); //这里的1就对应到onActivityResult()方法中的resultCode
            startActivity(display_info_intent);    //跳转到详情页面
        }else{
            //尚未登录
            Toast.makeText(getActivity(),"请先登录", Toast.LENGTH_LONG).show();
        }

    }
});
  •  打开之前写好的FindTabFragment.java,一样给热门文章和找文返回的文章列表添加点击监听,和上述代码一样,不同的是要判断是文章listItem还是找人返回的用户列表。在这里是通过隐藏的存放infoId的TextView来判断,是文章类型就存放ID,是用户列表的话就为空
//点击进入详情
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView getTvId = (TextView)view.findViewById(R.id.seek_list_cardId);
        if(getTvId.length()!=0){
            /*Toast.makeText(mActivity,"点击了"+getTvId.getText().toString()+"id", Toast.LENGTH_SHORT).show();*/
            SharedPreferences sp = mActivity.getSharedPreferences("save", Context.MODE_PRIVATE);
            Boolean isLogin = sp.getBoolean("isLogin",false);

            if(isLogin){
                //点击了进入详情
                Intent display_info_intent = new Intent();
                display_info_intent.setClass(mActivity, DisplayDetail.class);  //创建intent对象,并制定跳转页面
                display_info_intent.putExtra("infoId",getTvId.getText().toString());
                mActivity.setResult(1, display_info_intent); //这里的1就对应到onActivityResult()方法中的resultCode
                startActivity(display_info_intent);    //跳转到详情页面
            }else{
                //尚未登录
                Toast.makeText(getActivity(),"请先登录", Toast.LENGTH_LONG).show();
            }
        }
    }
});

——基本上从发现页、查找页跳转详情的功能就完成了,详情页显示如下

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值