Android 带有onClick弹出信息的Listview

Android 带有onClick弹出信息的Listview,android,popup,Android,Popup,我正在创建一个应用程序,它应该列出一些滑雪板技巧,当你点击一个技巧时,会出现一个弹出窗口,显示有关技巧的信息以及如何操作(包括文本和youtube视频) 如何实现这一点?以下是创建自定义列表适配器的代码,该适配器带有一个弹出窗口,当您在列表视图行中按某个图像时弹出该窗口: 这是适配器: public class tasksRepositoryAdapter extends ArrayAdapter<Task> { private ArrayList<Tas

我正在创建一个应用程序,它应该列出一些滑雪板技巧,当你点击一个技巧时,会出现一个弹出窗口,显示有关技巧的信息以及如何操作(包括文本和youtube视频)


如何实现这一点?

以下是创建自定义列表适配器的代码,该适配器带有一个弹出窗口,当您在
列表视图
行中按某个图像时弹出该窗口:

这是
适配器

    public class tasksRepositoryAdapter extends ArrayAdapter<Task>
{   
    private ArrayList<Task> list;

    public tasksRepositoryAdapter(Context context, int textViewResourceId, List<Task> tasksRepository) 
    {
        super(context, textViewResourceId, tasksRepository);
         this.list = new ArrayList<Task>();
         for (Task task : tasksRepository)
            {
                this.list.add(task);
            }
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View row;
        final ViewHolder holder = new ViewHolder();
        tfRobotoRegular = Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf");

        LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflator.inflate(R.layout.new_row, null);
        holder.tvTitle = (TextView) row.findViewById(R.id.text_title);
        String title = tasksRepository.get(position).getTitle();
        if (title.length()>25)
        {
            title = title.substring(0, 24);
            title = title + "...";
        }
        holder.tvTitle.setText(title);
        holder.tvTitle.setTypeface(tfRobotoRegular);
        holder.tvDate = (TextView) row.findViewById(R.id.text_date);
        holder.tvDate.setText(tasksRepository.get(position).getDate());
        holder.tvDate.setTypeface(tfRobotoRegular);
        holder.tvTime = (TextView) row.findViewById(R.id.text_time);
        holder.tvTime.setText(tasksRepository.get(position).getTime());
        holder.tvTime.setTypeface(tfRobotoRegular);
        holder.tvDescription = (TextView) row.findViewById(R.id.text_description);
        String description = tasksRepository.get(position).getDescription();
        if (description.length()>46)
        {
            description = description.substring(0, 45);
            description = description + "...";
        }
        holder.tvDescription.setText(description);
        holder.tvDescription.setTypeface(tfRobotoRegular);
        holder.tvId = (TextView) row.findViewById(R.id.text_id);
        holder.tvId.setText(String.valueOf(tasksRepository.get(position).getId()));
        holder.tvId.setTypeface(tfRobotoRegular);
        holder.tvLocation = (TextView) row.findViewById(R.id.text_location);
        holder.tvLocation.setText(tasksRepository.get(position).getCity());
        holder.llRowLayout = (LinearLayout) row.findViewById(R.id.llRowLayout);
        holder.imCalendar = (ImageView) row.findViewById(R.id.iCalendar);
        holder.imClock = (ImageView) row.findViewById(R.id.iClock);
        holder.imLocation = (ImageView) row.findViewById(R.id.iLocation);

        holder.imTaskStatusButton = (ImageView)   row.findViewById(R.id.iTaskStatusButton);
        holder.imTaskStatusButton.setTag(position);
        holder.imTaskStatusButton.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                 int[] location = new int[2];
                 currentRowId = position;
                 currentRow = v;    
                 // Get the x, y location and store it in the location[] array
                 // location[0] = x, location[1] = y.
                 v.getLocationOnScreen(location);

                 //Initialize the Point with x, and y positions
                 point = new Point();
                 point.x = location[0];
                 point.y = location[1];
                 showStatusPopup(TasksListActivity.this, point);
            }
        });

        String status = tasksRepository.get(position).getStatus();
        Log.d(TAG, "The status of the current row: "+ status );
        setStatusColorImages(status, holder.imClock, holder.imCalendar, holder.imLocation, holder.llRowLayout); 

        return row;
    }
}
static class ViewHolder
{
    RelativeLayout rlTitle;
    LinearLayout llRowLayout;
    TextView tvId;
    TextView tvTitle;
    TextView tvDate;
    TextView tvTime;
    TextView tvDescription;
    TextView tvLocation;
    ImageView imClock;
    ImageView imCalendar;
    ImageView imLocation;
    ImageView imTaskStatusButton;
}
// The method that displays the popup.
private void showStatusPopup(final Activity context, Point p) {

   // Inflate the popup_layout.xml
   LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llStatusChangePopup);
   LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View layout = layoutInflater.inflate(R.layout.status_popup_layout, null);

   // Creating the PopupWindow
   changeStatusPopUp = new PopupWindow(context);
   changeStatusPopUp.setContentView(layout);
   changeStatusPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
   changeStatusPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
   changeStatusPopUp.setFocusable(true);

   // Some offset to align the popup a bit to the left, and a bit down, relative to button's position.
   int OFFSET_X = -20;
   int OFFSET_Y = 50;

   //Clear the default translucent background
   changeStatusPopUp.setBackgroundDrawable(new BitmapDrawable());

   // Displaying the popup at the specified location, + offsets.
   changeStatusPopUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}
和弹出窗口:

    public class tasksRepositoryAdapter extends ArrayAdapter<Task>
{   
    private ArrayList<Task> list;

    public tasksRepositoryAdapter(Context context, int textViewResourceId, List<Task> tasksRepository) 
    {
        super(context, textViewResourceId, tasksRepository);
         this.list = new ArrayList<Task>();
         for (Task task : tasksRepository)
            {
                this.list.add(task);
            }
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View row;
        final ViewHolder holder = new ViewHolder();
        tfRobotoRegular = Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf");

        LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflator.inflate(R.layout.new_row, null);
        holder.tvTitle = (TextView) row.findViewById(R.id.text_title);
        String title = tasksRepository.get(position).getTitle();
        if (title.length()>25)
        {
            title = title.substring(0, 24);
            title = title + "...";
        }
        holder.tvTitle.setText(title);
        holder.tvTitle.setTypeface(tfRobotoRegular);
        holder.tvDate = (TextView) row.findViewById(R.id.text_date);
        holder.tvDate.setText(tasksRepository.get(position).getDate());
        holder.tvDate.setTypeface(tfRobotoRegular);
        holder.tvTime = (TextView) row.findViewById(R.id.text_time);
        holder.tvTime.setText(tasksRepository.get(position).getTime());
        holder.tvTime.setTypeface(tfRobotoRegular);
        holder.tvDescription = (TextView) row.findViewById(R.id.text_description);
        String description = tasksRepository.get(position).getDescription();
        if (description.length()>46)
        {
            description = description.substring(0, 45);
            description = description + "...";
        }
        holder.tvDescription.setText(description);
        holder.tvDescription.setTypeface(tfRobotoRegular);
        holder.tvId = (TextView) row.findViewById(R.id.text_id);
        holder.tvId.setText(String.valueOf(tasksRepository.get(position).getId()));
        holder.tvId.setTypeface(tfRobotoRegular);
        holder.tvLocation = (TextView) row.findViewById(R.id.text_location);
        holder.tvLocation.setText(tasksRepository.get(position).getCity());
        holder.llRowLayout = (LinearLayout) row.findViewById(R.id.llRowLayout);
        holder.imCalendar = (ImageView) row.findViewById(R.id.iCalendar);
        holder.imClock = (ImageView) row.findViewById(R.id.iClock);
        holder.imLocation = (ImageView) row.findViewById(R.id.iLocation);

        holder.imTaskStatusButton = (ImageView)   row.findViewById(R.id.iTaskStatusButton);
        holder.imTaskStatusButton.setTag(position);
        holder.imTaskStatusButton.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                 int[] location = new int[2];
                 currentRowId = position;
                 currentRow = v;    
                 // Get the x, y location and store it in the location[] array
                 // location[0] = x, location[1] = y.
                 v.getLocationOnScreen(location);

                 //Initialize the Point with x, and y positions
                 point = new Point();
                 point.x = location[0];
                 point.y = location[1];
                 showStatusPopup(TasksListActivity.this, point);
            }
        });

        String status = tasksRepository.get(position).getStatus();
        Log.d(TAG, "The status of the current row: "+ status );
        setStatusColorImages(status, holder.imClock, holder.imCalendar, holder.imLocation, holder.llRowLayout); 

        return row;
    }
}
static class ViewHolder
{
    RelativeLayout rlTitle;
    LinearLayout llRowLayout;
    TextView tvId;
    TextView tvTitle;
    TextView tvDate;
    TextView tvTime;
    TextView tvDescription;
    TextView tvLocation;
    ImageView imClock;
    ImageView imCalendar;
    ImageView imLocation;
    ImageView imTaskStatusButton;
}
// The method that displays the popup.
private void showStatusPopup(final Activity context, Point p) {

   // Inflate the popup_layout.xml
   LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llStatusChangePopup);
   LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View layout = layoutInflater.inflate(R.layout.status_popup_layout, null);

   // Creating the PopupWindow
   changeStatusPopUp = new PopupWindow(context);
   changeStatusPopUp.setContentView(layout);
   changeStatusPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
   changeStatusPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
   changeStatusPopUp.setFocusable(true);

   // Some offset to align the popup a bit to the left, and a bit down, relative to button's position.
   int OFFSET_X = -20;
   int OFFSET_Y = 50;

   //Clear the default translucent background
   changeStatusPopUp.setBackgroundDrawable(new BitmapDrawable());

   // Displaying the popup at the specified location, + offsets.
   changeStatusPopUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}
但我还不确定如何获得弹出窗口而不是意图。另外,我认为使用较新的API可以更容易地做到这一点,但我也不确定这一点。