Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中Listview實現點贊功能

Android中Listview實現點贊功能

編輯:關於Android編程

最近這段時間一直在看Android,利用Listview去實現點贊功能
基本思路:
進入界面–》獲取數據–》
在Listview中顯示–》
通過map集合(position,boolean)保存每一行是否被點擊–》
利用實體類去保存相應的對象–》
get/set方法進行相應值得改變–》
點擊一次,相應的數量加1

只實現了點贊功能,踩和贊基本類似。
具體實現如下:

繼承自BaseAdapter

package com.gz.test_listview;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gz on 2016/11/9.
 */
public class MainActivityAdapter extends BaseAdapter{
//用來獲取界面上的元素
    private ViewHolder holder;
    private Context context;
    //這裡的Bean是個測試用的內部類,將點贊數和踩得數存起來,可以定義一個類,
    //直接獲取數據就好,需要改進下
    private List praise_step_num = new ArrayList();

    private LayoutInflater inflater;
    //保存當前點贊數
    List> listItems;
    /*
    根據position,保存點擊的是哪一行數據,默認為false,點擊後改為true
    */
    private Map isExist = new HashMap();

    public MainActivityAdapter(Context context,List> listItems){
        this.context = context;
        this.listItems = listItems;
        inflater = LayoutInflater.from(context);
        Log.i("listItem",listItems.toString());
        init();
    }

    private void init() {
        /*
        為相應的數據賦值,所有的數據存儲在listItems中
        position一一對應每一行數據
         */
        for (int i = 0;i

MainActivity 進行Listview值賦值,然後進行和Adapter的綁定

package com.gz.test_listview;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class MainActivity extends Activity {

    private ListView listview;
    private String[] title = {"商用","家用","測試","多聯","空氣能","商用","家用","測試","多聯","空氣能"};
    private String[] praise = {"3","1","0","0","0","3","1","0","0","0"};
    private String[] step = {"3","0","0","0","0","3","0","0","0","0"};

    private ArrayList> map = new ArrayList>();

    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = (ListView) findViewById(R.id.listview);
        for(int i=0;i item = new HashMap();
            item.put("title",title[i]);
            item.put("praise",praise[i]);
            item.put("step",step[i]);
            map.add(item);
        }

        MainActivityAdapter adapter = new MainActivityAdapter(this,map);

        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this,DetailActivity.class);
                startActivity(intent);
            }
        });

    }
}

跳轉界面,測試用

package com.gz.test_listview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class DetailActivity extends Activity {


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

簡單的動畫效果的實現 直接用就好了

package com.gz.test_listview;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;

public class AnimationTools {
    public static void scale(View v) {
        ScaleAnimation anim = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        anim.setDuration(300);
        v.startAnimation(anim);

    }
}

界面




    


    

Listview子項



    <framelayout android:layout_height="wrap_content" android:layout_width="fill_parent">


        

        

            

            

                

                


            

            

                
                

                
                

            

        


    </framelayout>


點擊item後進入的界面




    
    
    
    
    



代碼中是我對程序的理解,有的地方可能不是很明白,還得完善完善 這篇文章給了我很大幫助,動畫效果就是搬過來的,哈哈,很不錯

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved