ListItem点击不在android中的片段中工作

时间:2016-02-24 11:01:17

标签: android listview android-fragments

我正在开发一个演示应用程序并在此演示中使用了片段,我在这个listView中有一个带自定义listItem的listView,我想捕获listItem的点击事件,我尝试过很多方法但是没有正在发生,anybuddy可以告诉我我的代码有什么问题,我的代码如下: 的 XML *

<?xml version="1.0" encoding="utf-8"?>
<one.tusk.stush.views.PostListItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin" >
    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:background="#ffffff" >
        <View android:layout_width="50dp"
            android:layout_height="200dp"
            android:layout_alignParentRight="true"
            android:id="@+id/goodThing"
            android:layout_below="@+id/buttonLikes"/>
        <one.tusk.stush.views.PostGridItem
            android:id="@+id/imageViewPostImage"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_marginTop="0dp"
            android:adjustViewBounds="false"
            android:contentDescription="Post image"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_launcher" />

            <ProgressBar
        android:layout_width="match_parent"
         android:layout_marginTop="70dp"
        android:layout_height="150dp"
        android:id="@+id/loadingPost"
         />

        <RelativeLayout
            android:id="@+id/profilelist"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_alignLeft="@id/imageViewPostImage"
            android:layout_alignRight="@id/imageViewPostImage"
            android:layout_below="@id/imageViewPostImage"
            android:alpha="1"
            android:background="#ffffff" >
            <RelativeLayout
                android:id="@+id/photorelative"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="7dp" >
                <ImageView
                    android:id="@+id/imageViewUserImage"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:adjustViewBounds="false"
                    android:contentDescription="User image"
                    android:maxHeight="30dp"
                    android:maxWidth="30dp"
                    android:minHeight="30dp"
                    android:minWidth="30dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/ic_launcher" />
                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/avatar_overlay1" />
            </RelativeLayout>
            <Button
                android:id="@+id/buttonComment"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:layout_toLeftOf="@+id/buttonLike"
                android:background="@null"
                android:drawableRight="@drawable/btn_comment" />
            <Button
                android:id="@+id/buttonShare"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="37dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="20dp"
                android:background="@null"
                android:drawableRight="@drawable/more" />
            <Button
                android:id="@+id/buttonLike"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/buttonShare"
                android:background="@null" />
            <TextView
                android:id="@+id/textViewUsername"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="55dp"
                android:paddingTop="10dp"
                android:text="ss"
                android:textColor="#0c5d55"
                android:textSize="15sp"
                android:textStyle="bold" />
        </RelativeLayout>
        <TextView
            android:id="@+id/textViewPostText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/profilelist"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:paddingBottom="5dp" />
        <RelativeLayout
            android:id="@+id/commentandlike"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textViewPostText">
            <TextView
                android:id="@+id/numbercomment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="10px"
                android:layout_toRightOf="@+id/buttonLikes"
                android:text="11111"
                android:textColor="#9f9f9f"
                android:textSize="14dp" />
            <Button
                android:id="@+id/buttonLikes"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="16dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="17dp"
                android:layout_weight="0.60"
                android:background="@null"
                android:text="Likes"
                android:textColor="#9f9f9f"
                android:textSize="14dp" />
            <TextView
                android:id="@+id/textViewTimeAgo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_marginRight="15dp"

                android:text="aggooooo"
                 android:textColor="#9f9f9f"
                android:textSize="14dp" />
            <View android:layout_width="1dp"
                android:layout_height="10dp"
                android:layout_below="@+id/buttonLikes"/>
        </RelativeLayout>
    </RelativeLayout>
</one.tusk.stush.views.PostListItem>

*

public class TimelineFragment extends BaseFragment implements LoaderManager.LoaderCallbacks<List<Post>>, OnRefreshListener, OnScrollListener,OnItemClickListener {

    public static ListView mListViewTimeline;

@Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        total = 0;
        mPullToRefreshLayout = (PullToRefreshLayout) getActivity().findViewById(R.id.ptr_layout);
        ActionBarPullToRefresh.from(getActivity()).allChildrenArePullable().listener(this).setup(mPullToRefreshLayout);

        mProgressBarLoading = (ProgressBar) getActivity().findViewById(R.id.progressBarLoading);
        mTextViewNoItems = (TextView) getActivity().findViewById(R.id.textViewNoItems);

        mTimelineAdapter = new TimelineAdapter(getActivity());

        mListViewTimeline = (ListView) getActivity().findViewById(R.id.listViewTimeline);
        mListViewTimeline.setEmptyView(mProgressBarLoading);
        mListViewTimeline.setAdapter(mTimelineAdapter);

        mListViewTimeline.setOnScrollListener(this);

        //  mListViewTimeline.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
        mListViewTimeline.setVerticalScrollBarEnabled(false);

        mListViewTimeline.setOnItemClickListener(this);

        final Activity activity = getActivity();

        if(activity != null) {
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    getLoaderManager().initLoader(0, null, TimelineFragment.this);
                    //Do something after 100ms
                }
            }, 1000);



            mTextViewNoItems.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View v) {
                    getLoaderManager().destroyLoader(0);

                    getLoaderManager().initLoader(0, null, TimelineFragment.this);
                    Log.d("CLICK", "REFRESH");

                }
            });
        }

    }
@Override
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {

        Intent intentPostDetail = new Intent(getActivity(), PostDetailActivity.class);
        Post post = mTimelineAdapter.getItem(position);
        intentPostDetail.putExtra("Post", post);
        startActivity(intentPostDetail);
    }
}

**timelineadapter**

package one.tusk.stush.adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.company.stush.R;
import one.tusk.stush.connect.Post;
import one.tusk.stush.views.PostListItem;

public class TimelineAdapter extends ArrayAdapter<Post> {

    private final LayoutInflater mInflater;

    public TimelineAdapter(Context context) {

        super(context, R.layout.list_item_post);
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setData(List<Post> data) {
        setNotifyOnChange(false); 
        clear();
        if (data != null) {
            addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) {

        Post post = getItem(position);
        PostListItem view;
        if (convertView == null) {
            view =  (PostListItem) mInflater.inflate(R.layout.list_item_post, parent, false);
        } else {
            view = (PostListItem) convertView;
        }
        view.setPost(post);
        return view;
    }
}

1 个答案:

答案 0 :(得分:-1)

而不是

@Override
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {

        Intent intentPostDetail = new Intent(getActivity(), PostDetailActivity.class);
        Post post = mTimelineAdapter.getItem(position);
        intentPostDetail.putExtra("Post", post);
        startActivity(intentPostDetail);
    }

试试这个

 mListViewTimeline.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

// do stuff here
}
});