试图在列表视图的一侧显示缩略图

时间:2018-06-18 03:48:35

标签: android listview

我目前有listview在选择目录后按字母顺序显示目录中的所有.cue文件,它使用外部程序来处理.cue文件。我想要做的是在背景中显示与imageview中的提示文件匹配的缩略图。例如,如果我在同一目录中有一个名为“a.cue”和“a.png”的文件,当listview中突出显示“a.cue”时, “a.png”同时显示为背景中的缩略图。这是我目前的代码:

public class openfileactivity7 extends Activity {

File path;
ListView list;
static ArrayList<String> bin_paths=new ArrayList<String>();
static ArrayList<String> bin_names=new ArrayList<String>();

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

    if (audioplay.isplayingAudio) {


    } else {audioplay.playAudio(this, R.raw.boot);

    }



    list=(ListView)findViewById(R.id.listView1);


    bin_paths.clear();
    bin_names.clear();


    path = new File("/mnt/usb_storage/USB_DISK0/udisk0/cue/pcecd");
    searchFolderRecursive1(path);




    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, bin_names);


    Collections.sort(bin_paths);
    Collections.sort(bin_names);

    adapter.sort(new Comparator<String>() {
        @Override
        public int compare(String lhs, String rhs) {
            return lhs.compareTo(rhs);
        }
    });



    list.setAdapter(adapter);


    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {

            String path = bin_paths.get(arg2);
            File file = new File(path);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/cue");
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(intent);
            if (audioplay.isplayingAudio) {
                audioplay.stopAudio();
            }
            startActivity(intent);
        }
    });
}




private static void searchFolderRecursive1(File folder)
{
    if (folder != null)
    {
        if (folder.listFiles() != null)
        {
            for (File file : folder.listFiles())
            {
                if (file.isFile())
                {

                    if(file.getName().contains(".cue"))
                    {
                        //   Log.e("ooooooooooooo", "path__="+file.getName());
                        file.getPath();
                        bin_names.add(file.getName());
                        bin_paths.add(file.getPath());
                        //    Log.e("bin_paths", ""+bin_names);
                    }
                }
                else
                {
                    searchFolderRecursive1(file);
                }
            }
        }
    }
}


}

1 个答案:

答案 0 :(得分:0)

尝试以下示例(未测试以下代码)。

1)MainActivity.class -------------------

public class MainActivity extends AppCompatActivity implements ItemAdapter.Listener {

private ListView lv;
private ItemAdapter itemAdapter;
private List<String> bin_names;
private List<String> bin_paths;
private List<String> bin_images_paths;
private static final String NO_IMAGE = "no_image";

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

    //Search for .cue files --- populate bin_names --- populate bin_path ---
    // check if an image (in the same directory) for the corresponding .cue file exists:
    // if yes add image path otherwise add NO_IMAGE.

    // done with data population part...

    lv = (ListView) findViewById(R.id.lv);
    itemAdapter = new ItemAdapter(getApplication() , bin_names , bin_paths , bin_images_paths , MainActivity.this);
    lv.setAdapter(itemAdapter);

}

@Override
protected void onResume() {
    super.onResume();
    if(itemAdapter != null){// refresh data
        itemAdapter.notifyDataSetChanged();
    }
}

@Override
public void PlayM(int position) {
    //open file
    // get file path by using bin_paths.get(position).
    File file = new File(bin_paths.get(position));
    if(file.exists()){
        if(file.getName().contains(".cue")){
            Toast.makeText(getApplicationContext() , "Play " + bin_paths.get(position) , Toast.LENGTH_LONG).show();
        }else{
            //refresh data.
            if(itemAdapter != null){
                itemAdapter.notifyDataSetChanged();
            }
        }
    }else{
        //refresh data.
        if(itemAdapter != null){
            itemAdapter.notifyDataSetChanged();
        }
    }

}
}

2)ItemAdapter.class ---------------------------

public class ItemAdapter extends BaseAdapter {

private Context mContext;
private LayoutInflater layoutInflater;
private Listener callback;

private List<String> n = new ArrayList<String>();
private List<String> p = new ArrayList<String>();
private List<String> i = new ArrayList<String>();


public ItemAdapter(Context c , List<String> n , List<String> p , List<String> i , Listener l) {
    mContext = c;
    callback = l;
    this.n = n; //names
    this.p = p; //paths
    this.i = i; //images;
    layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return p.size();
}

public Object getItem(int position) {
    return p.get(position);
}

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

public View getView(final int position, View convertView, ViewGroup parent) {
    TextView tv;
    ImageView iv;
    View view = convertView;


    if (convertView == null) {
        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.demo10, null);
        }
    }

    tv = (TextView) view.findViewById(R.id.tv);
    iv = (ImageView) view.findViewById(R.id.iv);

    File file = new File(p.get(position));
    if(file.exists()){
        if(!file.getName().contains(".cue")){// is not a .cue file
            n.remove(position);
            p.remove(position);
            i.remove(position);
            this.notifyDataSetChanged();
        }else{
            //everything is fine.
            tv.setText(n.get(position));
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(callback != null) {
                        callback.PlayM(position);
                    }
                }
            });

            File image = new File(i.get(position)); // check if image exists
            if(image.exists()){
                if(image.getName().contains(".jpg") || image.getName().contains(".png")){

                    Bitmap b = BitmapFactory.decodeFile(i.get(position));
                    iv.setImageBitmap(b);
                    iv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if(callback != null){
                                callback.PlayM(position);
                            }
                        }
                    });

                }else{
                    // hide imageView
                    iv.setVisibility(View.GONE);
                }
            }else{
                // hide imageView
                iv.setVisibility(View.GONE);
            }

        }
    }else{ // doesn't exist
        n.remove(position);
        p.remove(position);
        i.remove(position);
        this.notifyDataSetChanged();
    }

    return view;
}

public interface Listener {
    public void PlayM(int position);
}
}

3)demo11.xml --------------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv">
</ListView>

</android.support.constraint.ConstraintLayout>

4)demo10.xml -------------------

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tv"
    android:textSize="17sp"
    android:layout_centerVertical="true"
    android:layout_alignParentStart="true"
    android:layout_toStartOf="@id/iv"
    android:layout_marginStart="20dp"
    android:textStyle="bold"
    android:text="Name"/>

<ImageView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_weight="20"
    android:layout_centerVertical="true"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="10dp"
    android:id="@+id/iv"/>

</RelativeLayout>