J essai de récupérer la position du cursor depuis listview dans un fragment et de la passer a viewpager sur un autre fragment, avec cette position je veux me placer sur le même enregistrement mais aussi me deplacer sur les autres enregistrement en viewpager



l evenement onClick sur listview

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
@Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
 
 
    	super.onListItemClick(listView, view, position, id);
 
        // Get a URI for the selected item, then start an Activity that displays the URI. Any
        // Activity that filters for ACTION_VIEW and a URI can accept this. In most cases, this will
        // be a browser.
 
        // Get the item at the selected position, in the form of a Cursor.
       Cursor c = (Cursor) mAdapter.getItem(position);
        // Get the link to the article represented by the item.
        int  POSKEY = c.getPosition();
 
       // Intent i = new Intent(Intent.ACTION_VIEW, articleURL);
 
 
        Uri detailUri = Uri.parse(FeedContract.Entry.CONTENT_URI + "/" + id);
 
 
        WhatsOnFragment.newInstance(position,  detailUri);
 
 
        FragmentManager fm = getChildFragmentManager(); 
        WhatsOnFragment WWhatsOnFragment = new WhatsOnFragment();
         fm.beginTransaction().addToBackStack(null);
         fm.beginTransaction().replace(R.id.entry_list, WWhatsOnFragment).commit();  
 
 
 
 
    }


le fragament viewpager ou je veux recuperer la position

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
 
 
import com.example.android.network.sync.basicsyncadapter.provider.FeedContract;
import com.example.android.network.sync.basicsyncadapter.R;
 
 
import android.app.Activity;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.PluginState;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
 
 
public class WhatsOnFragment extends Fragment implements
        LoaderCallbacks<Cursor> {
 
	private static final String TAG="WhatsOnFragment";
 
	private OnItemSelectedListener mParentOnImageSelectedListener;
 
 
 
	private Handler mHandler = new Handler();
 
    private TextView mCountdownTextView;
    private ViewGroup mRootView;
    private Cursor mAnnouncementsCursor;
    private LayoutInflater mInflater;
    private int mTitleCol = -1;
    private int mDateCol = -1;
    private int mUrlCol = -1;
 
    //**********************************************''
 
    /** Column index for _ID */
    private static final int COLUMN_ID = 0;
    /** Column index for title */
    private static final int COLUMN_TITLE = 1;
    /** Column index for link */
    private static final int COLUMN_URL_STRING = 2;
    /** Column index for published */
 
 
    private static final int COLUMN_IMAG_LINK = 3;
 
    private static final int COLUMN_TEXT_ENTRY = 4;
    private static final int COLUMN_PUBLISHED = 5;
 
	 private static final String[] PROJECTION = new String[]{
         FeedContract.Entry._ID,
         FeedContract.Entry.COLUMN_NAME_TITLE,
         FeedContract.Entry.COLUMN_NAME_LINK,
 
         FeedContract.Entry.COLUMN_IMAG_LINK,
         FeedContract.Entry.COLUMN_TEXT_ENTRY,
         FeedContract.Entry.COLUMN_NAME_PUBLISHED
 };
 
	 public static final int POSITION_KEY = 0; 
     //***********************************************
 
 
    private static final int ANNOUNCEMENTS_LOADER_ID = 0;
 
 
 
    private Uri detailUri;
 
    public static int vpos;
 
    public static  WhatsOnFragment newInstance(int pos, Uri detailUri) {
    	WhatsOnFragment frag = new WhatsOnFragment();
		Bundle args = new Bundle();
		args.putInt("position", POSITION_KEY);
		args.putParcelable(FeedContract.Entry.CONTENT_ITEM_TYPE, detailUri);
		frag.setArguments(args);
		return frag;
	}
 
 
 
    @Override
    public void onAttach(Activity activity) {
     super.onAttach(activity);
     Log.v(TAG, "onAttach");
 
 
    }
 
 
 
 
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mInflater = inflater;
 
 
 
        mRootView = (ViewGroup) inflater.inflate(R.layout.content_f,
                container,false);
 
 
        refresh();
 
 
        return mRootView;
    }
 
 
    @Override
    public void onDetach() {
        super.onDetach();
       mHandler.removeCallbacks(mCountdownRunnable);
        getActivity().getContentResolver().unregisterContentObserver(mObserver);
    }
 
    private void refresh() {
        mHandler.removeCallbacks(mCountdownRunnable);
        mRootView.removeAllViews();
 
 
 
            setupDuring();
            //getLoaderManager().initLoader(0, null, this);
    }
 
 
 
    private void setupDuring() {
        // Start background query to load announcements
        getLoaderManager().initLoader(0, null, this);
        getActivity().getContentResolver().registerContentObserver(
        		FeedContract.Entry.CONTENT_URI, true, mObserver);
    }
 
    /**
     * Event that updates countdown timer. Posts itself again to
     * {@link #mHandler} to continue updating time.
     */
 
 
    private final Runnable mCountdownRunnable = new Runnable() {
        public void run() {
 
 
 
 
 
 
                 mHandler.postDelayed(new Runnable() {
                     public void run() {
 
 
                    	 refresh();
                     }
                 }, 100);
                 return;
 
        }
 
 
    };
 
 
 
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    	 return new CursorLoader(getActivity(),  // Context
                 FeedContract.Entry.CONTENT_URI, // URI
                 PROJECTION,                // Projection
                 null,                           // Selection
                 null,                           // Selection args
                // null);
 
                 FeedContract.Entry.COLUMN_NAME_PUBLISHED + " desc"); // Sort
    }
 
    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        if (getActivity() == null) {
            return;
        }
        if (data != null && data.getCount() > 0) {
            mTitleCol = data.getColumnIndex(FeedContract.Entry.COLUMN_NAME_TITLE);
            mDateCol = data.getColumnIndex(FeedContract.Entry.COLUMN_TEXT_ENTRY);
 
 
            showAnnouncements(data);
 
        }
    }
 
    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAnnouncementsCursor = null;
    }
 
    /**
     * Show the the announcements
     */
    private void showAnnouncements(Cursor announcements) {
        mAnnouncementsCursor = announcements;
 
 
        ViewGroup announcementsRootView = (ViewGroup) mInflater.inflate(
                R.layout.detail, mRootView, false);
 
        final ViewPager pager = (ViewPager) announcementsRootView.findViewById(
                R.id.pager);
 
 
        final PagerAdapter adapter = new AnnouncementsAdapter();
        pager.setAdapter(adapter);
        //pager.setCurrentItem(0);
 
        pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
 
 
    			pager.setCurrentItem(pager.getCurrentItem());
                //previousButton.setEnabled(position > 0);
                //nextButton.setEnabled(position < adapter.getCount() - 1);
 
 
 
            }
 
            @Override
    		public void onPageScrollStateChanged(int state) {
    			// TODO Auto-generated method stub
 
    		}
 
    		@Override
    		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    			// TODO Auto-generated method stub
    			//Toast.makeText(getActivity().getApplicationContext(),"myposition  " + position,Toast.LENGTH_LONG).show();
 
    		}
        });
 
 
        mRootView.removeAllViews();
        mRootView.addView(announcementsRootView);
    }
 
 
 
    public class AnnouncementsAdapter extends PagerAdapter {
 
        @Override
        public Object instantiateItem(ViewGroup pager, int position) {
 
            mAnnouncementsCursor.moveToPosition(position);
 
            View rootView = (View) mInflater.inflate(
                    R.layout.detail_fragment, pager, false);
            TextView titleView = (TextView) rootView.findViewById(R.id.title);
            TextView subtitleView = (TextView) rootView.findViewById(R.id.description);
 
 
            //WebView desc = (WebView) rootView.findViewById(R.id.desc);
 
    		// Enable the vertical fading edge (by default it is disabled)
    		ScrollView sv = (ScrollView) rootView.findViewById(R.id.sv);
    		sv.setVerticalFadingEdgeEnabled(true);
 
    		// Set webview properties
    		//WebSettings ws = desc.getSettings();
    		//ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
    		//ws.setLightTouchEnabled(false);
    		//ws.setPluginState(PluginState.ON);
    		//ws.setJavaScriptEnabled(true);
 
 
            titleView.setText(mAnnouncementsCursor.getString(mTitleCol));
            subtitleView.setText(mAnnouncementsCursor.getString(mDateCol));
 
            //desc.loadDataWithBaseURL("http://.../",mAnnouncementsCursor.getString(mDateCol), "text/html", "UTF-8", null);
 
 
            pager.addView(rootView, 0);
            return rootView;
        }
 
 
 
        @Override
        public void destroyItem(ViewGroup pager, int position, Object view) {
            pager.removeView((View) view);
        }
 
        @Override
        public int getCount() {
            return mAnnouncementsCursor.getCount();
        }
 
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }
 
        @Override
        public int getItemPosition(Object object) {
 
			return POSITION_NONE;
 
 
 
        }
    }
 
    private final ContentObserver mObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            if (getActivity() == null) {
                return;
            }
 
            Loader<Cursor> loader = getLoaderManager().getLoader(ANNOUNCEMENTS_LOADER_ID);
            if (loader != null) {
                loader.forceLoad();
            }
        }
    };
 
 
 
 
 
}