如何为 Android 中的列表设置缩略图
How to set thumbnails for the list in Android
下面是文件 browser.It 的代码,可以很好地浏览设备上的文件和文件夹。我想在 image.How 的名称旁边显示图像缩略图,我可以实现吗?如果有人可以帮助修改此代码以添加图像缩略图。谢谢
public class MainActivity extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
//带ListView的主布局
<LinearLayout 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"
android:orientation="vertical">
<TextView
android:id="@+id/path"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No Data"
/>
试试这个,
按如下方式为单个列表视图行创建布局,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="180dp"
android:layout_height="180dp"/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout >
将所需的图标放入可绘制文件夹并定义 class 以从 BaseAdapter
扩展
public class ListAdapter extends BaseAdapter{
private Context context;
private ArrayList<MyModel> data;
public AllListAdapter(Context con, ArrayList<String> dt) {
this.context = con;
this.data = dt;
}
// other overriden methods
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
TextView name;
ImageView image;
if (row == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.layout_for_single_list_row, null);
name = (TextView) row.findViewById(R.id.textview);
image = (ImageView) row.findViewById(R.id.image);
}
else
{
name = (TextView) row.findViewById(R.id.textview);
image = (ImageView) row.findViewById(R.id.image);
}
name.setText(data.get(position).getName());
image.setBackgroundDrawable(this.context.getResources().getDrawable(<your icon>))
return row;
}
}
设置数据到列表
ListAdapter allListadapter = new ListAdapter(this,item);
your_list.setAdapter(allListadapter);
下面是文件 browser.It 的代码,可以很好地浏览设备上的文件和文件夹。我想在 image.How 的名称旁边显示图像缩略图,我可以实现吗?如果有人可以帮助修改此代码以添加图像缩略图。谢谢
public class MainActivity extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
//带ListView的主布局
<LinearLayout 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"
android:orientation="vertical">
<TextView
android:id="@+id/path"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No Data"
/>
试试这个,
按如下方式为单个列表视图行创建布局,
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/image" android:layout_width="180dp" android:layout_height="180dp"/> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout >
将所需的图标放入可绘制文件夹并定义 class 以从 BaseAdapter
扩展public class ListAdapter extends BaseAdapter{ private Context context; private ArrayList<MyModel> data; public AllListAdapter(Context con, ArrayList<String> dt) { this.context = con; this.data = dt; } // other overriden methods @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View row = convertView; TextView name; ImageView image; if (row == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = inflater.inflate(R.layout.layout_for_single_list_row, null); name = (TextView) row.findViewById(R.id.textview); image = (ImageView) row.findViewById(R.id.image); } else { name = (TextView) row.findViewById(R.id.textview); image = (ImageView) row.findViewById(R.id.image); } name.setText(data.get(position).getName()); image.setBackgroundDrawable(this.context.getResources().getDrawable(<your icon>)) return row; } }
设置数据到列表
ListAdapter allListadapter = new ListAdapter(this,item); your_list.setAdapter(allListadapter);