我目前正在开发一个项目,我正在使用GridView。我想知道是否可以重新排列基于GridView的项目,范围从"最常点击"项目到"最少点击"项目
谢谢,
答案 0 :(得分:1)
对不起,这里的延迟是重新排列点击项目的代码。 我希望能满足你的需要。
ExampleData.java文件。
package com.example;
import java.io.Serializable;
import java.util.Comparator;
/**
* Created by Mustanser Iqbal on 2/6/2015.
*/
public class ExampleData implements Serializable {
private String name;
private String title;
int itemCount;
public ExampleData(String title,String name,int itemCount) {
this.name = name;
this.title = title;
this.itemCount = itemCount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getItemCount() {
return itemCount;
}
public void setItemCount(int itemCount) {
this.itemCount = itemCount;
}
// Comparator
public static class RearrangeData implements Comparator<ExampleData> {
@Override
public int compare(ExampleData arg0, ExampleData arg1) {
return arg0.itemCount - arg1.itemCount;
}
}
}
ExampleAdapter.java文件
package com.example;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
/**
* Created by Mustanser Iqbal on 2/6/2016.
*/
public class ExampleAdapter extends BaseAdapter {
private ArrayList<ExampleData> mExampleData;
private Context mContext;
private LayoutInflater inflater;
public ExampleAdapter(Context context, ArrayList<ExampleData> mExample) {
this.mContext = context;
this.mExampleData = mExample;
this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mExampleData.size();
}
@Override
public Object getItem(int position) {
return mExampleData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_item, null);
holder = new ViewHolder();
holder.titleTextView = (TextView) convertView.findViewById(R.id.title);
holder.nameTextView = (TextView) convertView.findViewById(R.id.name);
holder.countTextView = (TextView) convertView.findViewById(R.id.count);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.titleTextView.setText(mExampleData.get(position).getTitle());
holder.nameTextView.setText(mExampleData.get(position).getName());
holder.countTextView.setText(""+mExampleData.get(position).getItemCount());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mExampleData.get(position).setItemCount((mExampleData.get(position).getItemCount()+1));
Collections.sort(mExampleData, new ExampleData.RearrangeData());
Collections.reverse(mExampleData);
notifyDataSetChanged();
}
});
return convertView;
}
public class ViewHolder {
TextView titleTextView, nameTextView, countTextView;
}
}
MainActivity .java文件
package com.example;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Window;
import android.widget.GridView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
/**
* Created by Mustanser Iqbal on 2/6/2016.
*/
public class MainActivity extends AppCompatActivity {
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleData> exampleDataArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.header);
TextView actionBarText = (TextView) toolbar.findViewById(R.id.toolbar_title);
actionBarText.setText(getResources().getString(R.string.title));
setSupportActionBar(toolbar);
try {
getSupportActionBar().setDisplayShowTitleEnabled(false);
} catch (Exception e) {
e.printStackTrace();
}
// i Assume that this data is coming from server. or anywhere else.
exampleDataArrayList = new ArrayList<>();
exampleDataArrayList.add(new ExampleData("Mobile", "Android", 3));
exampleDataArrayList.add(new ExampleData("Mobile", "iPhone", 6));
exampleDataArrayList.add(new ExampleData("Mobile", "BlackBarry", 7));
exampleDataArrayList.add(new ExampleData("Mobile", "Windows", 9));
exampleDataArrayList.add(new ExampleData("Mobile", "Sony", 2));
exampleDataArrayList.add(new ExampleData("Laptop", "HP", 5));
exampleDataArrayList.add(new ExampleData("Laptop Charger", "XYZ Comapny", 8));
Collections.sort(exampleDataArrayList, new ExampleData.RearrangeData());
Collections.reverse(exampleDataArrayList);
GridView gridView = (GridView) findViewById(R.id.gridView);
mExampleAdapter = new ExampleAdapter(this, exampleDataArrayList);
gridView.setAdapter(mExampleAdapter);
}
}
action_bar.xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar_top"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_color"
android:minHeight="?attr/actionBarSize">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:textSize="20sp"
android:textColor="@color/header_text_color"
android:text="" />
</android.support.v7.widget.Toolbar>
activity_main.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include android:id="@+id/header"
layout="@layout/action_bar"/>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/header"
android:id="@+id/gridView"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:clipToPadding="true"
android:columnWidth="150dp"
android:numColumns="2"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:fastScrollEnabled="true"
android:focusable="true"
android:layout_marginRight="10dp"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:gravity="center"/>
</RelativeLayout>
grid_item.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="2dp"
android:background="@android:color/holo_green_dark">
<TextView
android:id="@+id/title"
android:text=""
android:textStyle="bold"
android:textColor="@android:color/white"
android:textSize="15sp"
android:padding="10dp"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:padding="10dp"
android:textSize="15sp"
android:gravity="start"
android:textColor="@android:color/white"
android:layout_centerHorizontal="true"
android:layout_below="@+id/title"/>
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:padding="10dp"
android:textSize="15sp"
android:textColor="@android:color/white"
android:gravity="center"
android:layout_alignParentEnd="true"
android:layout_below="@+id/title"/>
</RelativeLayout>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="bg_color">#686d77</color>
<color name="header_text_color">#64B5F6</color>
<color name="header_text_color_selected">#95caf4</color>
</resources>
的strings.xml
<resources>
<string name="app_name">Rearrange GridItems</string>
<string name="title">ReArrange GridItems Example</string>
</resources>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
</resources>