列表中的ListView单击侦听器

时间:2015-05-25 13:22:14

标签: android listview onitemclicklistener

我有一个listview1,其项目包含listview2,并且正常工作没有滚动问题。我面临的问题是listview1 onItemclickListener没有被调用。我已经尝试blocksDescendants focusable=false

listview1.setAdapter(adapter);
        listview1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent returnIntent = new Intent(this, NewPollActivity.class);
                returnIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                returnIntent.putExtra(IAppConstants.DEFAULT_DATA, rabbleMCQ);
                startActivity(returnIntent);
            }
        }); 

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   xmlns:app="http://schemas.android.com/apk/res/org.ssqueal.androidclient"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:descendantFocusability="blocksDescendants"
   android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/poll_creator_image_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="6dp" >

        <ImageView
            android:id="@+id/creator_image"
            style="@style/ContactImage" />

        <ImageView
            android:id="@+id/creator_private_image"
            style="@style/ContactImage"
            android:visibility="gone"
            app:border="true"
            app:border_color="#c6c6c6"
            app:border_width="0.5dp"
            app:shadow="false" />
    </RelativeLayout>

    <TextView
        android:id="@+id/creator_name"
        style="@style/ContactName"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/poll_creator_image_layout"
        android:fontFamily="sans-serif"
        android:maxLength="@integer/len_public_name" />

    <TextView
        android:id="@+id/creator_handle"
        style="@style/textview_small"
        android:layout_marginLeft="6dp"
        android:layout_marginTop="30dp"
        android:layout_toRightOf="@+id/poll_creator_image_layout"
        android:fontFamily="sans-serif"
        android:textColor="@color/light_blue" />

    <TextView
        android:id="@+id/poll_time"
        style="@style/textview_small"
        android:layout_alignParentRight="true"
        android:layout_marginRight="12dp"
        android:layout_marginTop="11dp"
        android:fontFamily="sans-serif"
        android:textColor="#8E9EAB" />

    <TextView
        android:id="@+id/poll_votes_count"
        style="@style/textview_small"
        android:layout_alignParentRight="true"
        android:layout_marginRight="12dp"
        android:layout_marginTop="31dp"
        android:fontFamily="sans-serif"
        android:textColor="#8E9EAB" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/questionImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-light"
        android:includeFontPadding="true"
        android:lineSpacingExtra="4dp"
        android:maxLength="256"
        android:paddingBottom="2dp"
        android:paddingLeft="12dp"
        android:paddingRight="12dp"
        android:paddingTop="2dp"
        android:textColor="@android:color/black"
        android:textSize="@dimen/textNormal" />
</RelativeLayout>

<it.sephiroth.android.library.widget.HListView
    android:id="@+id/poll_options_with_image_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:listSelector="@android:color/transparent"
    android:visibility="gone"
    app:hlv_dividerWidth="@dimen/marginSmall" />

<org.ssqueal.androidclient.views.CustomListView
    android:id="@+id/poll_options_list"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:cacheColorHint="#D3D3D3"
    android:clickable="true"
    android:divider="@null"
    android:dividerHeight="0dp"
    android:listSelector="@android:color/transparent"
    android:scrollingCache="true"
    android:visibility="gone" />

<TextView
    android:id="@+id/more_option_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="#8E9EAB"
    android:textSize="13sp"
    android:visibility="gone" />

适配器代码

public class DraftsRabbleAdapter extends BaseAdapter {

private final String TAG = "FeedsAdpater";
private LayoutInflater mInflater;
public ArrayList<RabbleMCQ> feedsData;
private Context context;
private int pollQuestionImageHeight;
private SSquealConfiguration mConfig;
private getRabbleFromDraftsCallback getDraftCallback;

public DraftsRabbleAdapter(Context context, ArrayList<RabbleMCQ> arr) {
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.feedsData = arr;
    this.context = context;
    pollQuestionImageHeight = UIUtil.getScreenWidth((Activity) context) / 2;
    mConfig = org.ssqueal.androidclient.SSquealApplication.getConfig(context);
    getDraftCallback = (getRabbleFromDraftsCallback) context;
}

public void addToFeedsData(ArrayList<RabbleMCQ> addRabbles) {
    feedsData.addAll(addRabbles);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
        final Holder wrapper;
        if (convertView == null)
        {
            convertView = mInflater.inflate(R.layout.layout_feeds_item, parent, false);
            wrapper = new Holder();
            wrapper.feedsItemContainer = (LinearLayout) convertView
                    .findViewById(R.id.feeds_item_container);
            wrapper.bottomLayoutOptions = (LinearLayout) convertView.findViewById(R.id.layout_feeds_item_bottom_options);
            wrapper.mcqPollLayout = (LinearLayout) convertView.findViewById(R.id.layout_message_poll_mcq);
            wrapper.poll_time = (TextView) convertView.findViewById(R.id.poll_time);

            wrapper.headerView=(RelativeLayout)convertView.findViewById(R.id.layout_poll_header_view);
            wrapper.creator_image = (ImageView) convertView.findViewById(R.id.creator_image);
            wrapper.creator_private_image = (ImageView) convertView.findViewById(R.id.creator_private_image);
            wrapper.creator_name = (TextView) convertView.findViewById(R.id.creator_name);
            wrapper.creator_handle = (TextView) convertView.findViewById(R.id.creator_handle);
            wrapper.question = (TextView) convertView.findViewById(R.id.question);
            wrapper.questionImage = (ImageView) convertView.findViewById(R.id.questionImage);
            wrapper.questionImage.getLayoutParams().height = pollQuestionImageHeight;
            wrapper.pollOptionsList = (CustomListView) convertView.findViewById(R.id.poll_options_list);
            wrapper.pollOptionsList.setExpanded(true);
            wrapper.more_option_text = (TextView) convertView.findViewById(R.id.more_option_text);
            wrapper.pollOptionsWithImageList = (HListView) convertView
                    .findViewById(R.id.poll_options_with_image_list);

            convertView.setTag(wrapper);
        }
        else
        {
            wrapper = (Holder) convertView.getTag();
        }
        final RabbleMCQ rabbleMCQ = feedsData.get(position);
        inflatePollMCQ(wrapper, rabbleMCQ, null, "rabble");

    return convertView;
}

private void inflatePollMCQ(Holder wrapper, RabbleMCQ rabbleMCQ, Comment comment, String actionType)
{
    wrapper.mcqPollLayout.setVisibility(View.VISIBLE);
    wrapper.poll_time.setText(rabbleMCQ.getTimestamp());
    wrapper.bottomLayoutOptions.setVisibility(View.GONE);
    inflatePollMCQ(wrapper, rabbleMCQ);
}

private void inflatePollMCQ(Holder wrapper, RabbleMCQ rabbleMCQ)
{
    if(rabbleMCQ.getRbl_scope().equalsIgnoreCase(IAppConstants.RABBLE_SCOPE_PUBLIC)) {
        wrapper.creator_image.setVisibility(View.VISIBLE);
        wrapper.creator_handle.setVisibility(View.VISIBLE);
        wrapper.creator_private_image.setVisibility(View.GONE);

        wrapper.creator_name.setText(rabbleMCQ.getCreator_profile().getPublic_name());
        wrapper.creator_handle.setText(rabbleMCQ.getCreator_profile().getPublic_handle());

        ImageLoader.getInstance().displayImage(rabbleMCQ.getCreator_profile().getPublic_image(), wrapper.creator_image,
                SSquealApplication.getImageOption(ImageType.PUBLIC_PROFILE), null);
    } else {
        wrapper.creator_handle.setVisibility(View.GONE);
        wrapper.creator_image.setVisibility(View.GONE);
        wrapper.creator_private_image.setVisibility(View.VISIBLE);

        //set private name if not empty else display the phone number.
        wrapper.creator_name.setText(TextUtils.isEmpty(rabbleMCQ.getCreator_profile().getPrivate_name()) == true ?
                rabbleMCQ.getCreator_profile().getPhoneNumber() : rabbleMCQ.getCreator_profile().getPrivate_name());

        ImageLoader.getInstance().displayImage(rabbleMCQ.getCreator_profile().getPrivate_image(), wrapper.creator_private_image,
                SSquealApplication.getImageOption(ImageType.PRIVATE_PROFILE), null);
    }
    if (!TextUtils.isEmpty(rabbleMCQ.getQuestion_image()))
    {
        //wrapper.questionImage.setColorFilter(Color.argb(150, 25, 25, 25));
        wrapper.questionImage.setVisibility(View.VISIBLE);
        wrapper.question.setTextColor(Color.WHITE);
        ImageLoader.getInstance().displayImage(rabbleMCQ.getQuestion_image(), wrapper.questionImage, SSquealApplication.getImageOption(ImageType.QUESTION), null);
        LayoutParams questionImageLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT,
                UIUtil.getScreenWidth((Activity) context) / 2);
        if (rabbleMCQ.getType() == RabbleType.MESSAGE)
        {
            questionImageLayoutParam.setMargins(context.getResources().getInteger(R.integer.margin_large), context.getResources().getInteger(R.integer.marginSmall), context.getResources().getInteger(R.integer.margin_large), context.getResources().getInteger(R.integer.marginSmall));
            questionImageLayoutParam.addRule(RelativeLayout.BELOW, R.id.question);
            wrapper.questionImage.setColorFilter(Color.TRANSPARENT);
            wrapper.question.setTextColor(Color.BLACK);
        }
        else {
            //wrapper.questionImage.setColorFilter(Color.argb(150, 25, 25, 25));
            questionImageLayoutParam.setMargins(0, 0, 0, 0);
        }
        wrapper.questionImage.setLayoutParams(questionImageLayoutParam);
    }
    else
    {
        // wrapper.question.setPadding(8, 0, 8, 0);
        wrapper.question.setTextColor(Color.BLACK);
        wrapper.questionImage.setVisibility(View.GONE);
    }
    if (TextUtils.isEmpty(rabbleMCQ.getQuestion_text()))
    {
        wrapper.question.setVisibility(View.GONE);
    }
    else
    {
        wrapper.question.setVisibility(View.VISIBLE);
        wrapper.question.setText(rabbleMCQ.getQuestion_text());
    }
    if (rabbleMCQ.getType() == RabbleType.MESSAGE
            || rabbleMCQ.getType() == RabbleType.OPEN_ENDED
            || (rabbleMCQ.getType() == RabbleType.RATING && rabbleMCQ.getOptions().size() == 1 && TextUtils
                    .isEmpty(rabbleMCQ.getOptions().get(0).getOptionBody())))
    {
        wrapper.pollOptionsWithImageList.setVisibility(View.GONE);
        wrapper.pollOptionsList.setVisibility(View.GONE);
        wrapper.more_option_text.setVisibility(TextView.GONE);
    }
    else
    {
        if (rabbleMCQ.isPollContainsImage())
        {
            PollOptionsHListAdapter pollOptionsHListAdapter = new PollOptionsHListAdapter(context,
                    rabbleMCQ.getOptions());
            pollOptionsHListAdapter.setSeperatorVisiblity(false);
            wrapper.pollOptionsWithImageList.setAdapter(pollOptionsHListAdapter);
            wrapper.pollOptionsWithImageList.setVisibility(View.VISIBLE);
            wrapper.pollOptionsList.setVisibility(View.GONE);
            wrapper.more_option_text.setVisibility(TextView.GONE);
        }
        else
        {
            PollMCQListAdapter pollMCQListAdapter = new PollMCQListAdapter(context, rabbleMCQ.getOptions());
            pollMCQListAdapter.setOptionLimited(true);
            wrapper.pollOptionsList.setAdapter(pollMCQListAdapter);
            if (rabbleMCQ.getOptions().size() > 3)
            {
                wrapper.more_option_text.setVisibility(TextView.VISIBLE);
                wrapper.more_option_text.setText("+" + (rabbleMCQ.getOptions().size() - 3) + " more options");
            }
            else
                wrapper.more_option_text.setVisibility(TextView.GONE);
            wrapper.pollOptionsList.setVisibility(View.VISIBLE);
            wrapper.pollOptionsWithImageList.setVisibility(View.GONE);
        }
    }
}
@Override
public int getCount() {
    return feedsData.size();
}

@Override
public Object getItem(int arg0) {
    return feedsData.get(arg0);
}

@Override
public long getItemId(int arg0) {
    return arg0;
}

class Holder
{
    private LinearLayout feedsItemContainer;
    private String userId = null;
    private ImageView iv_like,iv_followPerson,reply_on_poll,iv_rerabble,more;
    private TextView more_option_text, tv_action;
    private ImageView creator_image, creator_private_image;
    private TextView creator_name, creator_handle;
    private LinearLayout mcqPollLayout;
    private HListView pollOptionsWithImageList;
    private CustomListView pollOptionsList;
    private ImageView questionImage;
    private TextView question;
    private TextView poll_time;
    private TextView poll_votes_count;
    private TextView poll_like_count;
    private TextView poll_rerabble_count;
    private TextView poll_comment_count;
    private LinearLayout bottomLayoutOptions;
    private RelativeLayout headerView;
    public void startUserProfileActivity()
    {
        Intent intent = new Intent(context, UserProfileActivity.class);
        intent.putExtra(IAppConstants.DEFAULT_DATA, this.userId);
        context.startActivity(intent);
    }
}

public void trackException(Throwable e) {
    e.printStackTrace();
    Tracker t = ((SSquealApplication) ((Activity) context).getApplication()).getTracker(TrackerName.APP_TRACKER);
    t.send(new HitBuilders.ExceptionBuilder()
      .setDescription(new StandardExceptionParser(context, null)
              .getDescription(Thread.currentThread().getName(), e))
      .setFatal(false)
      .build()
  );
}
}

1 个答案:

答案 0 :(得分:0)

你必须像这样<{1}}开始活动

Intent

如果你想将任何东西传递给另一个活动,你必须像对此一样将其作为参数添加到Intent

Intent intent = new Intent(Activity1.this, Activity2.class);
startActivity(intent);

并且请检查一次..您是否在AndroidManifest.xml文件中添加了此intent.putExtra("DATA", List.get(position));